当子组件有嵌套的多个父组件时,从子组件调用超父函数
Calling to super parent function from child component when it has nested multiple parents
让我们假设一个React组件有多个父组件。当我想调用父级组件(最高级组件)的函数时。我怎么能做到呢?请参阅示例代码。
' ' '
export default class WeekView extends React.Component {
constructor(props) {
super(props);
}
// this functions has to be called from third child
loadData() {
var data = [];
this.setState({events : data});
}
render() {
return (
<ChildOne >
);
}
}
export class ChildOne extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<ChildTwo />
);
}
}
export class ChildTwo extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<ChildThree />
);
}
}
export class ChildThree extends React.Component {
constructor(props) {
super(props);
}
addEvent() {
// how to call loadData() function, which is included in the highest parent
}
render() {
return(
<Button onCLick={this.addEvent.bind(this)} />
);
}
}
' ' '
是的。我们可以通过props将请求发送到层次结构的顶端。但是有没有别的方法可以用别的方法来做,除非通过层次结构。提前谢谢你。
反应的方式是从父组件通过props向下传递功能。你说这是你的答复。
基本上你的父级会有一个函数传递给子级然后触发它
相关文章:
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 如何控制组件'的createContent函数被激发
- 如何在渲染函数中检查 react 中子组件的状态
- ember-js组件初始化不同的函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 带有内置图像对象的Canvas组件构造函数;t显示's图像
- 如何在单击组件时调用函数
- 当写入“;函数“;在ReactJS组件中
- ES6调用扩展组件函数
- 主干调用在 React 组件函数中不起作用
- 在 Knockout 中初始化时调用组件函数
- Requirejs带有Knockout错误:无法处理绑定“;组件:函数(){return f}"-匿名定义()
- ReactJS组件函数
- 反应:“this”在组件函数中是未定义的
- 调用Coldfusion组件函数的jQuery-post方法不起作用
- 配置子组件函数,将多个参数传递给父函数
- 在Reactjs中,从函数内部调用组件函数
- 需要新语法的React/Redux组件函数
- 如何从javascript函数内部调用React组件函数
- 从dangouslysetinnerhtml中的onclick调用React组件函数