在React js中道具从父母到孩子还是从孩子到父母

时间:2019-07-15 16:59:17

标签: reactjs constructor

我很困惑,我们说react js具有单向流,这意味着道具只能从父级发送到子级。但是为什么我们要写super(props); ??

通常我们写

class Test extends React.Component { 
    constructor(props) 
    { 
        super(props); 
        this.state = { hello : "World!" }; 
    }
}

那我们为什么要从孩子向父母发送道具? 我希望我的问题得到解释

1 个答案:

答案 0 :(得分:1)

  

但是为什么我们要编写super(props);

这与组件的父/子关系无关。这与React.Componentclass Test extends React.Component之间的超类/子类关系有关。 super(props)调用React.Component构造函数,并传递道具。