reactjs es6调用其他函数内的函数抛出未定义的函数
reactjs es6 calling function inside other function throws function not defined
在 App.js 文件中有一个按钮组件。我正在调用 hi 函数 onclick 事件。在这个事件中,我想使用 redux saga 来调用 sayHello();
如果我直接在按钮单击事件上使用 sayHello,代码可以正常工作。但我的要求是在另一个函数中调用它。以下是 reference.Appreciate 您的帮助的完整文件。
您应该在组件范围内定义您的函数。
因为函数SayHello
定义在props
.
就像这样:
class App extends Component {
hi = () => {
onsole.log("hiiiiiiiiiiiiii");
this.props.sayHello();
}
render() {
const { hello, exampleTask } = this.props;
return (
<div className="App">
<h1>Hello {hello}</h1>
<h2>Redux-saga demo. Start editing to see some magic happen!</h2>
<button onClick={this.hi}>Say Hello!</button>
<button onClick={exampleTask}>Example Task</button>
</div>
);
}
}
在另一个函数中调用某个函数时,必须指定作用域。
如下使用:
const hi = () => {
console.log("hiiiiiiiiiiiiii");
this.sayHello();
};
希望对您有所帮助!
在 App.js 文件中有一个按钮组件。我正在调用 hi 函数 onclick 事件。在这个事件中,我想使用 redux saga 来调用 sayHello();
如果我直接在按钮单击事件上使用 sayHello,代码可以正常工作。但我的要求是在另一个函数中调用它。以下是 reference.Appreciate 您的帮助的完整文件。
您应该在组件范围内定义您的函数。
因为函数SayHello
定义在props
.
就像这样:
class App extends Component {
hi = () => {
onsole.log("hiiiiiiiiiiiiii");
this.props.sayHello();
}
render() {
const { hello, exampleTask } = this.props;
return (
<div className="App">
<h1>Hello {hello}</h1>
<h2>Redux-saga demo. Start editing to see some magic happen!</h2>
<button onClick={this.hi}>Say Hello!</button>
<button onClick={exampleTask}>Example Task</button>
</div>
);
}
}
在另一个函数中调用某个函数时,必须指定作用域。 如下使用:
const hi = () => {
console.log("hiiiiiiiiiiiiii");
this.sayHello();
};
希望对您有所帮助!