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

>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.

Leave a Reply