Home > Enterprise >  momentjs gives Uncaught Error: Objects are not valid as a React child
momentjs gives Uncaught Error: Objects are not valid as a React child

Time:08-02

I'm trying to make my own calendar with momentjs, I have been following the docs on momentjs but it keeps giving me this error: Uncaught Error: Objects are not valid as a React child

this is the line that gives the error: moment([2010, 0, 31]).add(1, 'months');

I have console.log'ed it and I get an object instead of a string which gives the error, but it's supposed to give a string.

I have made a codeSandBox here

CodePudding user response:

You need to convert your moment date object to a string before display using format().

In your codesandbox, I can't see the error (I guess you removed the problem by not rendering it), but wherever you put this date object in your react render JSX, use format before display:

moment([2010, 0, 31]).add(1, 'months').format()

format accepts an argument that describes the output format if you want to manually define this.

This is tangential, but you should be aware moment is a bit outdated despite its legacy popularity. See https://momentjs.com/docs/ under "Project Status".

You might consider using something slimmer and more modern whilst you still can.

  • Related