0

I am new in Reactjs and i am working with nextjs, i tried to integrate "newsletter", I created a component for this which is working fine but i am unable to display "success" or "error" message in screen/WebsitePage,i am getting "success" message in console but "success/error" message not showing Here is my code

import React, { useEffect, useState } from "react";

const Subscribe = () => {
 const [email, setEmail] = useState('');
 const [error, setError] = useState('');
 const [success, setSuccess] = useState('');

const subscribeMe = async (event) => {
            const emails = email;
            event.preventDefault();
            const res = await fetch('https://example.com/admin-panel/Api/subscribe?email='+emails);
            const { error, message } = await res.json();
            if (error) {
                    console.log(error);
                    setError(error);
            } else {
                    console.log(message);
                    setSuccess(message);
            }
};

And i put following code just below my form close,But nothing is showing in my screen,How can i do this ?

{success 
    ? 
        <span className="flex items-center text-sm font-bold text-green-700"> 
        {success}
    </span> 
    : 
<span className="flex items-center text-sm font-bold text-red-800">
{error} 
</span>
1
  • Please provide the full code in stackoverflow code snippet, or a live editor like codesandbox. Commented Jun 29, 2022 at 18:10

2 Answers 2

1

Actually, you have not taken the value from JSON properly. I have posted a demo code that is working exactly as you wanted.

Also, you have not used === operator for comparing string true. The status value of JSON is not in boolean it's a string.

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [error, setError] = useState("");
  const [success, setSuccess] = useState("");

  const subscribeMe = async (event) => {
    const res = await fetch(
      "https://mocki.io/v1/65583d53-9071-47af-b7d7-840e88763d08"
    );

    const data = await res.json();
    const error = data["status"];
    const message = data["msg"];

    if (error === "false") {
      console.log(error);
      setError(error);
    } else {
      console.log(message);
      setSuccess(message);
    }
  };
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <span>
        {" "}
        {error} {success}
      </span>
      <br />
      <button onClick={subscribeMe}>Press me </button>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Here is a demo URL https://yfv7c3.csb.app/

Sign up to request clarification or add additional context in comments.

1 Comment

your code gave me solution,Thank you so much for help :)
0

It might be due to trying to render an object, try to JSON.stringify the data

8 Comments

as i said i am new in nexjs so can you update code so i can check and implement in my side
Hey, Ritika can you show us your JSON response so that I can try it on my laptop. Anyway, I have tried the dummy code and it's working fine for me. pastebin.com/Epwt1DeR
@NalinNishant: whenever i hit api i am getting following message as json response {"status":true,"msg":"Email saved successfully"}
K give me 5 mins i am trying
@NalinNishant: i am also trying , according to me something wrong with following code I just need to change correct if else condition ( means if we use same email id then giving us message "{"status":false,"msg":"Email already exist"} " so i just need to use if else condition according to response return, current code is if (error) { const error='errror'; setError(error); } else { const message='subscribed successfully'; console.log(message); setSuccess(message); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.