[英]How do I pass my functions through two components?
So I have a state : 所以我有一个状态:
toggleWatched:false
These are the four functions in App.js file that will be passed through to the MovieCard component : 这些是App.js文件中的四个函数,这些函数将传递给MovieCard组件:
watchedMovie= () => {
console.log("WATCHED MOOPIE");
}
notWatchedMovie = () => {
console.log("HAVENT WATCHED MOOPIE");
}
handleChange= () => {
this.setState({toggleWatched:!this.state.toggleWatched})
}
Toggling = () => {
this.state.toggleWatched ? this.watchedMovie : this.notWatchedMovie;
}
Before passing to the MovieCard it has to be passed through the favorites component : 在传递给MovieCard之前,必须先通过收藏夹组件传递它:
<Favorites
{...props}
handleChange={this.handleChange}
Toggling={this.Toggling}
toggleWatched={this.state.toggleWatched}
/>
Then in favorites.js's render : 然后在favorite.js的render中:
const {
Toggling,
handleChange,
toggleWatched,
} = this.props;
Then it is passed to the movie card which is a constant component : 然后将其传递给电影卡,该卡是一个常量组件:
<MovieCard
handleChange={handleChange}
Toggling={Toggling}
toggleWatched={toggleWatched}
/>
In the movie card it is used like : 在电影卡中,它的用法如下:
const MovieCard = ({ handleChange,Toggling,})
I made a button in MovieCard which handles it : 我在MovieCard中做了一个按钮来处理它:
<button style={{fontSize:"14px"}} onClick={handleChange}>
{
Toggling
}
</button>
When I press the button, there's no error but nothing is consoled as well. 当我按下按钮时,没有错误,但是也没有任何控制台。 I don't know what the error is.
我不知道这是什么错误。 Can someone help me fix this?
有人可以帮我解决这个问题吗?
I think I know the problem. 我想我知道问题所在。 Your function
Toggling
is not being invoked. 您的功能
Toggling
未被调用。 A function will not be invoked by itself, you are only passing handleChange
to the button onClick
at MovieCard
and so only function handleChange
being invoked. 函数本身不会被调用,你只有经过
handleChange
到该按钮onClick
在MovieCard
只等功能handleChange
被调用。 And what it does is only setState
for toggleWatched
. 它所做的只是对
toggleWatched
setState
。
From your case, your handleChange
function should be as following 根据您的情况,您的
handleChange
函数应如下所示
handleChange= () => {
this.setState({toggleWatched:!this.state.toggleWatched}, () => this.Toggling())
}
We are passing this.Toggling()
as a second parameter of setState
because this.setState
is asynchronous 我们将
this.Toggling()
作为setState
的第二个参数传递,因为this.setState
是异步的
Your problem is the way you use function. 您的问题是使用功能的方式。 Missing ()
失踪 ()
Toggling = () => {
this.state.toggleWatched ? this.watchedMovie() : this.notWatchedMovie();
}
And button in MovieCard must be use 而且必须使用MovieCard中的按钮
<button style={{fontSize:"14px"}} onClick={handleChange}>
<Toggling />
</button>
Or 要么
<button style={{fontSize:"14px"}} onClick={handleChange}>
{Toggling()}
</button>
Update: The button must have content, so I suggest U change MovieCard like that 更新:该按钮必须具有内容,所以我建议您像这样更改MovieCard
<div>
<button style={{fontSize:"14px"}} onClick={handleChange}>
Click
</button>
<Toggling />
</div>
First of all, this will not be a good approach if you call your function like 首先,如果您像这样调用函数,这将不是一个好方法
<button style={{fontSize:"14px"}} onClick={handleChange}>
{
Toggling()
}
</button>
What should be done is, you should call Toggling after the call of handleChange
. 应该做的是,您应该在
handleChange
调用之后调用handleChange
。 Something like: 就像是:
watchedMovie= () => {
console.log("WATCHED MOOPIE");
}
notWatchedMovie = () => {
console.log("HAVENT WATCHED MOOPIE");
}
handleChange= () => {
this.setState({toggleWatched:!this.state.toggleWatched}, this.Toggling);
}
Toggling = () => {
this.state.toggleWatched ? this.watchedMovie() : this.notWatchedMovie();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.