How can i keep state in a React component using ES6

前端 未结 4 1232
生来不讨喜
生来不讨喜 2020-12-22 09:25

I\'m trying to use a stateful React component with ES6 but when I define a constructor the constructor will only be called once while the component is rendered multiple time

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 10:04

    You SubComponent should be:

     class SubComponent extends React.Component {
          constructor(props) {
            super(props);
            console.log("Creating sub component");
          }
    
          render() {
            return (
    count: {this.props.count}
    ); } }

提交回复
热议问题