最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React updateState function - Stack Overflow

programmeradmin2浏览0评论

I have simple update data function which currently not working:

    class App extends React.Component {

           constructor(props) {
              super(props);

              this.state = {
                 data: 'Initial data...'
              }

              this.updateState = this.updateState.bind(this);

           };

           updateState() {
              this.setState({data: 'Data updated...'})
           }

           render() {
              return (
                 <div>
                    <button onClick = {this.updateState}>CLICK</button>
                    <h4>{this.data}</h4>
                 </div>
              );
           }
        }

   ReactDOM.render(<App/>, document.getElementById('app'));

Below I have link to jsbin:

,js,output

I have simple update data function which currently not working:

    class App extends React.Component {

           constructor(props) {
              super(props);

              this.state = {
                 data: 'Initial data...'
              }

              this.updateState = this.updateState.bind(this);

           };

           updateState() {
              this.setState({data: 'Data updated...'})
           }

           render() {
              return (
                 <div>
                    <button onClick = {this.updateState}>CLICK</button>
                    <h4>{this.data}</h4>
                 </div>
              );
           }
        }

   ReactDOM.render(<App/>, document.getElementById('app'));

Below I have link to jsbin:

http://jsbin./vidumiroki/edit?html,js,output

Share Improve this question edited Sep 13, 2016 at 13:21 Davin Tryon 67.3k15 gold badges147 silver badges137 bronze badges asked Sep 13, 2016 at 13:20 user6795995user6795995 1
  • In what way is it not working? Please include the details in your question. – David L Commented Sep 13, 2016 at 15:11
Add a ment  | 

1 Answer 1

Reset to default 4

You missed state in you return render function

class App extends React.Component {

   constructor(props) {
      super(props);

      this.state = {
         data: 'Initial data...'
      }

      this.updateState = this.updateState.bind(this);

   };

   updateState() {
      this.setState({data: 'Data updated...'})
   }

   render() {
      return (
         <div>
            <button onClick = {this.updateState}>CLICK</button>
            <h4>{this.state.data}</h4>
         </div>
      );
   }
}

ReactDOM.render(<App/>, document.getElementById('app'));
发布评论

评论列表(0)

  1. 暂无评论