I'm a newbie at react.js and was going through some code samples to understand the declarations. However, I came across a scenario where it complains about
getInitialState(): must return an object or null
I cross checked the syntax and everything looked right until I found out that introducing a new line break is causing the issue. Being a front end developer ( spent many years with javascript ) , it doesn't make sense why a simple line break can change the syntax.
Declaration 1 : ( throws error in the console ) https://jsfiddle.net/69z2wepo/68177/
var Test = React.createClass({
getInitialState : function(){
return
{
counter : 1
}
},
render:function()
{
return (
<div> {this.state.counter} </div>
);
}
});
ReactDOM.render(<Test />,
document.getElementById('container'));
Declaration 2 : ( Works fine ) https://jsfiddle.net/69z2wepo/68178/
var Test = React.createClass({
getInitialState : function(){
return { // line break was removed
counter : 1
}
},
render:function()
{
return (
<div> {this.state.counter} </div>
);
}
});
ReactDOM.render(<Test />,
document.getElementById('container'));
I would appreciate if someone can help me understand this weird behavior.