Greetings to all!

It seems everything should work fine, but when compiling I get the error: Uncaught TypeError: Cannot read property 'state' of undefined .

The details are written:

 React.createElement( 'ul', { id: 'list-drafts' }, undefined.state.drafts.length > 0 ? undefined.state.drafts.map(function (draft) { return React.createElement( 'li', 

That part of the source in question:

 var ReviewsWidgetSidebar = React.createClass({ getInitialState: () => { return { drafts: [] }; }, componentDidMount: () => { GetApi.call('data/get-drafts', {}, function (drafts) { this.setState({ drafts: drafts }); }); }, render: () => { return ( <div> <ul id="list-drafts"> { (this.state.drafts.length > 0) ? this.state.drafts.map((draft) => { return ( <li data-id={draft.id} data-is-unread={draft.is_unread}> {draft.text} </li> ); }) : ` <li style={{textAlign: 'center'}}> Loading... <img src="/img/loading.gif" /> </li> ` } </ul> </div> ); } }); 

I tried to correct the error in different ways (make the mapping higher, override this with another variable, etc.), but it does not work ...

Tell me what's wrong, how to fix it?

  • Why not use React.Component instead of React.createClass ? - YozhEzhi
  • @YozhEzhi, and need? Judging by the documentation, it should work in this form completely. - Stanislav
  • skip the link to the React.createClass documentation. I now remember that it will soon be depricated. - YozhEzhi
  • @YozhEzhi the most interesting thing is that you seem to be right ... translated the code to ES6 + and it worked as expected. - Stanislav

0