0

I have a React app that consumes a nested JSON object retrieved by an sync AJAX call. On return of the call, I traverse the JSON and bind each of the packets of data the components need to individual state variables.

I then call the components in render - also nested - and pass them the data they and their children need as props.

Passing individual values works well via <SubComponent dataToUse={this.props.value} /> However when I pass an array and attempt to .map that array in the component, the browser console gives me the error:

Cannot read property 'map' of undefined

...which obviously seems that the array is not actually there - since it is in render, perhaps it is running map before the AJAX call returns the JSON file? However, if I stringify the array data when binding it to state and pass that, the whole array of data is there, as a string.

If I take that string and parse it back into JSON, I get this:

Unexpected token u in JSON at position 0

Which again suggests that the data is NOT there.

Suffice it say, I am confused. I can easily pass single data variables down my app hierarchy as props but cannot access arrays.

Has anyone else had this sort of problem?

Edit; some code:

My top most component makes an AJAX call with JQuery:

$.ajax({
    type: 'GET',
    url: "/api/" + eventCode,
    dataType: 'json',
    cache: false,
    success: function(data) {
        this.setState({updateList: data.updates});
        }.bind(this)
    });

This AJAX call sits in its own function and is called every 30 seconds here:

componentDidMount: function() {
    this.loadCommentsFromServer();
    var refreshInterval = 32000; // take this from the blob!
    setInterval(this.loadCommentsFromServer, refreshInterval);  
},

The render function is as follows:

render: function() {
    return (
        <div id='App'>
            <UpdateList updateData={this.state.updateList}/>
        </div>
    )
}

The UpdateList component is as follows:

var UpdateList = React.createClass({

    render: function() {


        return (
            <div id='UpdateList'>
                Update list:
                <ul>
                {
                    this.props.updateData.map(function(update, i){
                        return <Updates singleUpdate={this.props.updateData[i]}/>
                    })
                }
                </ul>
            </div>
        )
    }
});

If add a simple test array in getInitialState, the array is mapped and the Updates component renders each item just as desired.

15
  • Not really, but if you show some code I might be able to help. Commented May 18, 2016 at 18:50
  • Added code from Main and UpdateList components Commented May 18, 2016 at 19:03
  • So what do you get if you console.log this.state.updateList in the topmost render method? Commented May 18, 2016 at 19:09
  • since your request is async, the first time you render that component you wont have an array there. So you do need an initial array there. Is there a problem in doing something like updateList: [] on the getInitialState? Commented May 18, 2016 at 19:12
  • @hansn logging this.state.updateList in the render method of the main component (where the ajax call is made) happily outputs the entire array that I'm after. Commented May 18, 2016 at 19:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.