The change of the react subcomponent state triggers the render function. How does the parent component perceive the change of the subcomponent?

the child component clicks the button to change its state to trigger the render update, but does not change the state state of the parent component, so how does the parent component sense that the child component has been re-render?

parent component code

import React from "react";
import TodoItem from "./TodoItem.js";
class TodoList extends React.Component{
    constructor(){
        super();
    }
    render(){
        return (
            <div>
                <TodoItem/>
            </div>
        )
    }
    
    
}
export default TodoList;

subcomponents

import React from "react";

class TodoItem extends React.Component{
    constructor(){
        super();
        this.state = {
            greet:"hi"
        }
        this.handlerChange = this.handlerChange.bind(this);
    }
    render(){
        return (
            <div>
                <div>{this.state.greet}</div>
                <button onClick={this.handlerChange}>state</button>
            </div>
        )
    }
    handlerChange(){
        this.setState(()=>{
            return{
                greet:"hello"
            }
        })
    }
}
export default TodoItem;
May.12,2022

the communication between the parent component and the child component uses props, and the communication between the child component and the parent component uses the callback function


if the parent component needs to listen for changes in the child component, it is recommended to move the state to the parent component


to pass a method similar to onChange to the child component. If the child component status update, call onChange , onChange define in the parent component. Don't you know that the subcomponents are updated

Menu