Render notification component on screen

Asked

Viewed 45 times

0

Hello, I’m trying to render a notification component on the screen when the Ubmit button is clicked, however I’m returning the component directly in Return, it’s not working because it’s the wrong way, but I don’t know how it’s the right way to do it.

Basically I have a Submit function, in it I pass another function called HandleError, this function returns a component Notificationthat I created, it is working perfect because I tested it elsewhere, but when putting it in Return nothing happens when the Submit button is clicked, I would like to know why. Thank you from now on.

Note: I’m not returning anything in htlm, just the function HandleSubmitby clicking the button.

const [input, setInput] = useState("");

async function handleSubmit(e) {
e.preventDefault();
const data = input;
try {
  const response = await service.create(data);
  handleError(response.status);
  return response;
} catch (err) {
  handleError(err.response.status);
  }
}

function handleError(statusCode) {
  if (statusCode === 201)
    return (
      <Notification
        title="Teste"
        description="teste do teste"
        type="success"
       />
     );
else
  return (
    <Notification title="Teste" description="teste do teste" type="error" />
  );
}

Code for my notification component

function Notification({ title, description, type }) {
 return (
   <div>
     {notification[type]({
       message: title,
       description: description,
     })}
   </div>
 );

}

Basically it is this notification that will appear on the screen if the form submission works and everything ok

componente de notificaçao

  • Please enter the entire code of your component. The way the question is, you can’t know exactly what’s going on or what you want to happen.

  • Hello, I updated the question and put the component code and the expected return of it, if you do not understand something else that warns that I try to explain, I am waiting for help Thank you!

  • Pedro, how is the style of your component? How should it appear on the screen? As a modal? There are several strategies there. One of them is to control whether this component is visible or not if it is already in the DOM or even using the Portal which is a concept where this element is rendered "outside" of the DOM.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.