Ad

React Formatting Dates With Momentjs Server Side

- 1 answer

I am using reactjs with an asp.NET MVC5 project and I'm trying to format dates in my react components using momentjs. I'm using server side rendering and I have the momentjs library included in ReactConfig.cs. It seems on the server side as soon as it tries to render the component it doesn't understand what moment is.

How I am trying to use it:

<Input onChange={e => this.handleModelChange(e, 'RowCreated')} type="text" label="Created Date" placeholder="Created Date" readOnly="true" value={moment(this.state.model.RowCreated).parseZone().format('l LT')} />

I also tried making a getter function but it is the same issue.

The error message is: Error while rendering "ComponentName" to "react1": ReferenceError: moment is not defined

I know that if I give the ReactConfig.cs an invalid path to the momentjs library it will show an error message, so that leads me to believe the path is currently correct and that the library is indeed loading on the server side. Underscorejs is correctly working on both server side and client side in the same manner.

Is there a simple solution to this? I feel like I could be missing something silly here.

Ad

Answer

I upgraded momentjs from version 2.9.0 to 2.11.0 and it worked as expected using the code example I posted in the original question. It looks like they changed how they do their closure so I'm guessing that is related.

Ad
source: stackoverflow.com
Ad