1

I have this:

var Astronomy = React.createClass({
getDefaultProps: function() {
    return {meteo : JSON.parse(localStorage.getItem('meteo')).data};
},
render: function() {
    return (
    <div className="temps">
    {this.props.meteo.weather.map(function(d, i) {return
        <div className="waqt">
            <div className="temps">
                <div className="raise">
                    <div className="sunraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunrise"]}</i></div>
                    <div className="sunset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunset"]}</i></div>
                </div>
                <div className="set">
                    <div className="moonraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonrise"]}</i></div>
                    <div className="moonset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonset"]}</i></div>
                </div>
            </div>
        </div>
    }
    )}
    </div>
    );
},
componentDidMount: function() {
return console.log(this.props.meteo.weather[0]["astronomy"][0]["sunrise"]);
},
});

But I get an empty result ! even the console gives what I expect 06:19 AM, and debugging it using chrome extension, I see that the array stayed as it is like in the screenshot:

Screenshot

1
  • if someone came here, React warns when you dont use a key, add this <div className="waqt" key={index}> Commented Sep 11, 2015 at 2:43

2 Answers 2

2

JavaScript will insert a semicolon after return if it is followed by a line break. I.e.

function foo() {
  return
  42
}

is the same as

function foo() {
  return;
  42
}

i.e. the last line will never be evaluated and undefined will be returned.

The return value always has to be or start at the same line as the return statement:

return (
  <div>...</div>
);

Also there is no need to access the data as this.props.meteo.weather[i]. That value is already passed to the callback as d, so you can just do d.astronomy[0].sunrise. Learn more about .map in the MDN documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

wonerful! not only worked, but I got a wonderful course about map and about the evil semicolon, thank you !
1
var Astronomy = React.createClass({
getDefaultProps: function() {
    return {meteo : JSON.parse(localStorage.getItem('meteo')).data};
},
render: function() {
    return (
    <div className="temps">
    {this.props.meteo.weather.map(function(d, i) {
    return <div className="waqt">
            <div className="temps">
                <div className="raise">
                    <div className="sunraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunrise"]}</i></div>
                    <div className="sunset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunset"]}</i></div>
                </div>
                <div className="set">
                    <div className="moonraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonrise"]}</i></div>
                    <div className="moonset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonset"]}</i></div>
                </div>
            </div>
        </div>
    },this )}
    </div>
    );
},
componentDidMount: function() {
return console.log(this.props.meteo.weather[0]["astronomy"][0]["sunrise"]);
},
});

this has change in the map function,your can appoint it by the second argument,or use ()=> ES6 arrow function.

5 Comments

but why it works even without this ? Edit: adding this, makes it in an inifinte loop that crashes the browser (Chrome)
since i dont have the complete code , i cant run it in your situation. here is my demo jsfiddle.net/znd4ose3/1
can you edit the answer and explain the role of this, because it is unclear, since it worked without, but in this example when i remove it, it doesent work.
map is called by this.props.meteo.weather,so , in the function ,this is this.props.meteo.weather
ah, so using the d as argument did the same trick, thank you again!

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.