我正在开发React应用程序,并制作了一个类似于框架的东西,其中我有一个包装器组件,类似这样的东西。
class FrameworkComponent extends React.Component {
someFunction() {
// send data to child data using childs function
// something like this.some.thing.childFunction("mydata");
...
}
render() {
return (
<div>
<div><button onClick={this.someFunction}>Click me</button></div>
<div>{this.props.child}</div>
</div>
)
}
}并像这样使用它:
class SecondComponent extends React.Component {
childFunction(dataRecived) {
alert(dataRecived);
}
render() {
return <div>Hello world</div>;
}
}
import FrameworkComponent from '../FrameworkComponent';
import SecondComponent from '../SecondComponent';
class OtherComponet extends React.Component {
render() {
return (
<div>
<FrameworkComponent>
<div><SecondComponent /></div>
</FrameworkComponent>
</div>
)
}
}因此,在这里,我希望子组件通过更新它的子props:componentWillReceiveProps()或调用它的子方法从父包装组件接收数据。
发布于 2018-02-06 19:20:28
最好的方法是使用HOC - Higher - https://reactjs.org/docs/higher-order-components.html
https://stackoverflow.com/questions/48641497
复制相似问题