0

new to react, have seen some of the similar title as below but still could not clarify with the error getting on the console saying Uncaught TypeError: this.state.stock.map is not a function . When I use the similar code in the another api call then there is no such error coming. The code is as below for the same. If I am assigning data from the get method to stock the also its not working and giving the same issue. What is the problem ? a few links that I referred.

Link 1 Link 2

Image after discussion.

var StockData = React.createClass({
      getInitialState: function() {
        return {
            stock: []
        };
      },

      componentDidMount: function() {
        this.serverRequest = $.get(this.props.source, function (data) {
           var old = JSON.stringify(data).replace("//", ""); //convert to JSON string
           var obj = JSON.parse(old);                        //convert back to JSON
           console.log(obj);
           this.setState({
              stock: obj
            });
        }.bind(this));
      },

      componentWillUnmount: function() {
        this.serverRequest.abort();
      },

      render: function() {
        return (
                React.createElement("div",null,
                        React.createElement("ul",null,
                            this.state.stock.map(function (listValue){
                                return React.createElement("li",null,listValue.t,"( ",listValue.e," )-"," Current Price :",listValue.l_cur,"  Date :",listValue.lt
                                );
                            })
                        )
                    )
        );
      }
    });

    ReactDOM.render(React.createElement(StockData, { source: "http://www.google.com/finance/info?q=NSE:ATULAUTO,WIPRO,INFY" }),document.getElementById('proper-list-render3'));
Community
  • 1
  • 1
Harshit C.
  • 71
  • 2
  • 8

2 Answers2

0

Replace render function with stored this as self

 render: function() {
        var self = this;
        return (
                React.createElement("div",null,
                        React.createElement("ul",null,
                            self.state.stock.map(function (listValue){
                                return React.createElement("li",null,listValue.t,"( ",listValue.e," )-"," Current Price :",listValue.l_cur,"  Date :",listValue.lt
                                );
                            })
                        )
                    )
        );
      }
Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21
0

I finally got the solution to the problem. The data received after calling the url is received in the string format and not JSON. The correct code is as below and now it is working fine. Thanks @Piyush.kapoor for the help.

  componentDidMount: function() {
        this.serverRequest = $.get(this.props.source, function (data) {     
           var old = data.replace("//", "");   
           this.setState({   
                    stock: JSON.parse(old)   
               });
        }.bind(this));
      }
Harshit C.
  • 71
  • 2
  • 8