Displaying date object as react child

I’ve started with react hooks(new programmer) since everybody said it is easier to start with, i’m trying to print on the screen deliverydate but react does not accept object as react child, is there easy solution? This is what I have:

<p>{something?.something2?.deliveryDate}</p>

I’m trying to get this:

deliveryDate: Tue Mar 09 2021 13:10:04 GMT+0200 (Eastern European Standard Time) {}

I get this error: Uncaught Error: Objects are not valid as a React child (found: Tue Mar 09 2021 13:10:04 GMT+0200 (Eastern European Standard Time)). If you meant to render a collection of children, use an array instead.

Answer

You need to pass react something it does know how to render, like a string. So if deliveryDate is a Date then

<p>{something?.something2?.deliveryDate.toString()}</p>

will work. If you want that exact format, you might need to use something like moment.js

Leave a Reply

Your email address will not be published. Required fields are marked *