使用 ES6 在 React 中将一个函数传递给多个子组件
Passing a function down multiple children components in React using ES6
我有一个LayoutComponent
,PageComponent
,和SingleComponent
。
当我的用户单击按钮时,我想在应用程序使用 Meteor FlowRouter
路由到的NewPageComponent
上向用户显示一条消息。
为此,我将消息存储在 LayoutComponent
的状态中,然后将此消息和handlerMethod
作为 props 传递给 SingleComponent
PageComponent
,这是一个无状态的功能组件。
我没有任何运气将handlerMethod
正确传递给SingleComponent
以便在LayoutComponent
上设置消息状态。
我知道这是一个简单的语法问题,但有人可以帮助我找到我的错误吗?
布局组件:
export default class LayoutComponent extends Component {
constructor() {
super();
this.state = {
message: null
};
this.handlerMethod = this.handlerMethod.bind(this);
}
handlerMethod(message) {
this.setState({ message: message });
}
render() {
// the PageComponent is actually passed via FlowRouter in this.props.content, so it needs to be cloned to add props
let contentWithProps = React.cloneElement(this.props.content, { message: this.state.message, handlerMethod: ((message) => this.handlerMethod) });
return (
<div>{contentWithProps}</div>
);
}
}
页面组件:
const PageComponent = ({ message, handlerMethod }) => {
return (
<div>
<SingleComponent message={message} handlerMethod={(message) => handlerMethod} />
</ div>
);
}
元件:
export default class SingleComponent extends Component {
constructor() {
super();
this.state = {
};
this.handleButtonClick = this.handleButtonClick.bind(this);
}
handleButtonClick(event) {
event.preventDefault();
// do some more stuff...
this.props.handlerMethod("You pressed the button!");
FlowRouter.go("newPage");
}
render() {
<div>
<button onClick={this.handleButtonClick}>button</button>
</div>
}
}
您实际上并没有在发布的代码中调用您的handlerMethod
。所以这个:
handlerMethod: ((message) => this.handlerMethod)
应为:
handlerMethod: ((message) => this.handlerMethod(message))
或者更简单地说:
handlerMethod: this.handlerMethod
你的错误在于你传递了一个将返回this.handlerMethod
而不是调用它的函数:
handlerMethod: ((message) => this.handlerMethod) // this returns the handlerMethod function, doesn't call it
应该是
handlerMethod: ((message) => this.handlerMethod(message))
您也可以直接传递它:
handlerMethod: this.handlerMethod
直接传递它的原因是因为你在构造函数中绑定了handlerMethod
的上下文以进行LayoutComponent
,这意味着handlerMethod
内部的this
将被固定为LayoutComponent
(见下面的注释(
下面是一个简短的示例:
布局组件
export default class LayoutComponent extends Component {
constructor() {
// ...
this.handlerMethod = this.handlerMethod.bind(this); // handler is bound
}
handlerMethod(message) {
// ...
}
render() {
let contentWithProps = React.cloneElement(
this.props.content, {
message: this.state.message,
handlerMethod: this.handlerMethod // pass the handler directly
}
);
return <div>{contentWithProps}</div>;
}
}
页面组件
// pass the handlerMethod directly
const PageComponent = ({ message, handlerMethod }) => {
return (
<div>
<SingleComponent message={message} handlerMethod={handlerMethod} />
</ div>
);
}
单组分
export default class SingleComponent extends Component {
// ...
handleButtonClick(event) {
// ...
this.props.handlerMethod("You pressed the button!"); // call the passed method here
// ...
}
// ...
}
注意:从技术上讲,您可以通过调用new this.handlerMethod
来覆盖绑定this
,但这并没有发生,所以你没事。
如果你只是想向下传递方法,你可以这样做:
const PageComponent = ({ message, handlerMethod }) => {
return (
<div>
<SingleComponent message={message} handlerMethod={handlerMethod} />
</ div>
);
}
相关文章:
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 如何控制组件'的createContent函数被激发
- 如何在渲染函数中检查 react 中子组件的状态
- ember-js组件初始化不同的函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 带有内置图像对象的Canvas组件构造函数;t显示's图像
- 如何在单击组件时调用函数
- 当写入“;函数“;在ReactJS组件中
- ES6调用扩展组件函数
- 将事件绑定到其他UI组件's函数,在Kendo MVVM中
- 如何调用React组件's函数(Coherent UI)
- 在jQuery就绪函数上未完成Primefaces布局组件
- 如何使用Angular 1.5组件将属性求值为字符串,这是一个自定义函数
- 如何在react组件构造函数中设置Interval
- 如何绑定'这'到react类之外的函数,后者是来自其他组件的回调
- 如何在作为“prop”传递给子组件的函数中使用“this”
- React:将props传递给函数组件
- 用javascript重写react函数组件
- 不能为无状态函数组件提供引用
- “无状态函数组件”与“ES6类组件”相比有什么优势