Search code examples
reactjsreact-statereact-class-based-component

How to update multiple child states in React Class Components independently?


I know how it can be done with Functional Components. But when it comes to class components, I'm having few questions to be clarified.

I've a class here,

class MyTable extends Component {
   constructor(props) {
      super(props);
       this.state = {
          page:0,
          rowsPerPage:10
       }
   }

   handleChangePage(event) {
      //Here I want to update only **page** keeping **rowsPerPage** intact
   }

   handleChangeRowsPerPage(event) {
      //Here I want to update only **rowsPerPage** keeping **page** intact
   }

   render() {
      return(
         <SomeComponent
           onChangePage={this.handleChangePage}
           onChangeRowsPerPage={this.handleChangeRowsPerPage}
         />
      )
   }
}
export default Mytable;

So here what I want to know is,

  1. If I want to update only page inside the state object, should I have to preserve rowsPerPage and update them both as this.setState({page:<updatedValue>, rowsPerPage:<preservedValue>); And Vice versa

  2. What code goes inside handleChangePage and handleChangeRowsPerPage, if we can update independent properties inside a state object.

  3. What's the best practice when we've several such states and we want to update each one independently?


Solution

  • You can update page and rowsPerPage independently as I did bellow. You have just to call this.setState and passing and object with the key of state you want to update

    class MyTable extends React.Component {
       constructor(props) {
          super(props);
           this.state = {
              page:0,
              rowsPerPage:10
           }
           
           this.handleChangePage = this.handleChangePage.bind(this);
           this.handleChangeRowsPerPage = this.handleChangeRowsPerPage.bind(this);
       }
    
       handleChangePage(event) {
          //Here I want to update only **page** keeping **rowsPerPage** intact
          this.setState({page: event.target.value});
       }
    
       handleChangeRowsPerPage(event) {
          //Here I want to update only **rowsPerPage** keeping **page** intact
          this.setState({rowsPerPage: event.target.value});
       }
    
       render() {
          return (
             <div>
                <div>
                  Page <input type="text" value={this.state.page} onChange={this.handleChangePage} />
                </div>
                
                <div>
                  rowsPerPage <input type="text" value={this.state.rowsPerPage} onChange={this.handleChangeRowsPerPage} /></div>
             </div>
          )
       }
    }
    
    ReactDOM.render(<MyTable />, document.getElementById('root'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <div id="root"></div>