ReactJS: setState on parent inside child component

后端 未结 7 1278
孤街浪徒
孤街浪徒 2020-12-02 10:18

What is the recommended pattern for doing a setState on a parent from a child component.

var Todos = React.createClass({
  getInitialState: function() {
             


        
7条回答
  •  暖寄归人
    2020-12-02 10:48

    If you are working with a class component as parent, one very simple way of passing a setState to a child is by passing it within an arrow function. This works as it sets a hoisted environment that can be passed around:

    class ClassComponent ... {
    
        modifyState = () =>{
            this.setState({...})   
        }
        render(){
              return <>
        }
    }
    

提交回复
热议问题