1

So I am trying to create a component that I can use anywhere in my app. I am not entirely sure how to explain it accurately but I'll use the sweet alert library as an example.

So in swal, you can simply call something like this and the component would render itself.

import swal from 'sweetalert';

swal("Hello world!");

How can I replicate that into my own component so I could call it like

ConfirmationDialog({
     message: 'test',
     visibility: true,
     onConfirm: handleOnConfirm,
     onCancel: handleOnCancel
})

1 Answer 1

1

It's basically in one of the first points in the react documentation.
State and Lifecycle
At least if I understood your question correctly.

First example on the above website:

const root = ReactDOM.createRoot(document.getElementById('root'));
  
function tick() {
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  root.render(element);
}

setInterval(tick, 1000);
Sign up to request clarification or add additional context in comments.

3 Comments

been trying to do this actually however the problem is the page is already rendered and I cannot call root.render like on your example, I basically want to append the component on the DOM without calling root.render.
Rendering function/Component without a call from render function Conditional Rendering Maybe those two links can help you. But as far as I know you can't render an element without calling the render function. How is react supposed to know that you want to render an element? Only one option comes to my mind: Add the root element as a parameter to your function.
thank you for your insight, I may have an idea for a solution to my problem. thanks!

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.