Skip to content
Advertisement

Unable to display Date in JSX in ReactJS

Following is my code which is working fine if I initialize test with a string, though if I changed it to new Date() its throwing error. Let me know what I am doing wrong as I just started with React.

Code –

JavaScript

Error –

Objects are not valid as a React child (found: Fri Jul 21 2017 02:11:18 GMT+0530 (India Standard Time)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.

Advertisement

Answer

You are seeing that because Date is an object not a string. To get it to work as you have it with no libraries:

JavaScript

Call the toString() method on the date object.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement