Answers for "handling state in functional components reactjs ijnterview questions"

0

handling state in functional components reactjs ijnterview questions

function ClassRoom(props){
 let [studentsCount,setStudentsCount] = useState(0);

 const addStudent = () => {
   setStudentsCount(++studentsCount);
 }
 return(
   <div>
     <p>Number of students in class room: {studentsCount}</p>
     <button onClick={addStudent}>Add Student</button>
   </div>
 )
}
Posted by: Guest on May-21-2021
0

handling state in class components reactjs ijnterview questions

class ClassRoom extends React.Component{
          constructor(props){
            super(props);
            this.state = {studentsCount : 0};
         
            this.addStudent = this.addStudent.bind(this);
          }
         
          addStudent(){
            this.setState((prevState)=>{
              return {studentsCount: prevState.studentsCount++}
            });
          }
         
          render(){
            return(
              <div>
                <p>Number of students in class room: {this.state.studentsCount}</p>
                <button onClick={this.addStudent}>Add Student</button>
              </div>
            )
          }
         }
Posted by: Guest on May-21-2021

Code answers related to "handling state in functional components reactjs ijnterview questions"

Code answers related to "Javascript"

Browse Popular Code Answers by Language