handling state in functional components reactjs ijnterview questions

Solutions on MaxInterview for handling state in functional components reactjs ijnterview questions by the best coders in the world

showing results for - "handling state in functional components reactjs ijnterview questions"
Jazmín
04 May 2017
1function ClassRoom(props){
2 let [studentsCount,setStudentsCount] = useState(0);
3
4 const addStudent = () => {
5   setStudentsCount(++studentsCount);
6 }
7 return(
8   <div>
9     <p>Number of students in class room: {studentsCount}</p>
10     <button onClick={addStudent}>Add Student</button>
11   </div>
12 )
13}
14
Alejandra
24 Oct 2019
1class ClassRoom extends React.Component{
2          constructor(props){
3            super(props);
4            this.state = {studentsCount : 0};
5         
6            this.addStudent = this.addStudent.bind(this);
7          }
8         
9          addStudent(){
10            this.setState((prevState)=>{
11              return {studentsCount: prevState.studentsCount++}
12            });
13          }
14         
15          render(){
16            return(
17              <div>
18                <p>Number of students in class room: {this.state.studentsCount}</p>
19                <button onClick={this.addStudent}>Add Student</button>
20              </div>
21            )
22          }
23         }
24