Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to render a component with window alert

enter image description herei try to render a component of all the products that are in the cart with window alert but i get just ‘object object ‘ even when the component is empty.
enter image description hereenter image description here

i tried to pass it to a function but the result remains the same

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can not render a component in alert method.

You should pass a string to window.alert().

For example ✅:

window.alert("Hello! I am an alert box!!");

And the below is the wrong way ❌:

window.alert(<Cart />);

If you want to render the <Cart /> in a modal you should build it yourself or find another solution.

More explanation:
The reason you get [object object] is that React components are all objects with some properties. And you can’t pass an object to alert methods.

Although you can stringify the object using JSON.stringify.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading