[英]What does it mean myFunction is not a function?
I'm passing storyLike function from parent to child.Sometimes when onPress is clicked I get following error: this.props.storyLike(props.story) is not a function. 我正在将storyLike函数从父级传递给子级。有时,单击onPress时出现以下错误:this.props.storyLike(props.story)不是函数。 I thought it's a context issue but couldn't resolve it.
我认为这是一个上下文问题,但无法解决。
Here is my Parent: 这是我的父母:
renderRow(item) {
return (
<Story
story={item}
openStory={this.props.openStory}
getProfile={this.props.getProfile}
storyLike={this.props.storyLike}
/>
);
}
Here is child component Story, just passing props to StoryFooter component: 这是子组件Story,只是将道具传递给StoryFooter组件:
class Story extends Component {
render() {
return (
<View style={styles.container}>
<StoryHeader {...this.props} />
<StoryContent {...this.props} />
<StoryFooter {...this.props} storyViewType={"feed"} />
</View>
);
}
}
Bellow is child component, I'm triggering storyLike from onPress changeLikedStatus function: 波纹管是子组件,我从onPress changeLikedStatus函数触发了storyLike:
class StoryFooter extends Component {
state = { isLiked: false, likes: 0 };
changeLikedState(props) {
const numberOflikes = this.state.isLiked
? this.state.likes - 1
: this.state.likes + 1;
if (this.state.isLiked) {
this.setState({ isLiked: false, likes: numberOflikes });
} else {
this.setState({ isLiked: true, likes: numberOflikes });
}
this.props.storyLike(props.story);
}
<TouchableOpacity onPress={() => this.changeLikedState(this.props)}>
This is how your parent should look like 这就是你父母的样子
renderRow(item) {
return (
<Story
// ... other props
storyLike={(prop) => this.props.storyLike(prop)}
/>
);
}
This way you explicitly tell that the child will be passing a prop to the function. 这样,您可以明确地告诉孩子将向该函数传递一个prop。
Hopefully your storyLike
function is an arrow function that looks like this: 希望您的
storyLike
函数是一个箭头函数,如下所示:
storyLike = (prop) => {
// code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.