Maria Jane Maria Jane - 2 months ago 18
React JSX Question

failed to display state value in react

I got blank using getInitialState, must be something is wrong but I don't know where.

var counter = React.createClass({
getInitialState(){
return {
count: 0
}
},
render() {
return(
<p>value of my counter is: {this.state.count}</p>
)
}
})

ReactDOM.render(
<counter />,
document.getElementById('container')
);


Jsfiddle https://jsfiddle.net/b9bu3ssz/

Answer

Component should be Counter, not counter

var Counter = React.createClass({
  getInitialState(){
    return {
      count: 0
    }
  },
  render() {
    return <p>value of my counter is: {this.state.count}</p>
  }
})

ReactDOM.render(
  <Counter />,
  document.getElementById('container')
);