React native 使用道具从堆栈导航器选项调用redux操作
我想从堆栈导航器选项中的redux调用一个操作。这是我的密码。当我使用this.props.action_名称使用动作时,它表示这不是一个函数。我做错了什么?我使用navigation.params实现了这一点,但仍然不起作用React native 使用道具从堆栈导航器选项调用redux操作,react-native,React Native,我想从堆栈导航器选项中的redux调用一个操作。这是我的密码。当我使用this.props.action_名称使用动作时,它表示这不是一个函数。我做错了什么?我使用navigation.params实现了这一点,但仍然不起作用 componentDidMount() { this.props.navigation.setParams({ referencedSharePost: this.sharePost, referencedDoShare: this.pro
componentDidMount() {
this.props.navigation.setParams({
referencedSharePost: this.sharePost,
referencedDoShare: this.props.doShare
});
}
sharePost = () => {
this.props.doShare();
console.log("DONE");
};
render() {
return (
<View style={styles.container}>
<ScrollView>
<WritePost profile={this.state.loggedUserProfile} />
<View style={styles.sharePostWrapper}>
<PostProfileBar profile={this.state.postedUserProfile} />
<Image
source={{
uri: "https://pbs.twimg.com/media/DWvRLbBVoAA4CCM.jpg"
}}
resizeMode={"stretch"}
style={styles.image}
/>
</View>
</ScrollView>
</View>
);
}
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerTitle: "Share To Feed",
headerTitleStyle: {
paddingLeft: "20%",
paddingRight: "20%"
},
headerStyle: {
paddingRight: 10,
paddingLeft: 10
},
headerLeft: (
<Icon
name={"close"}
size={30}
onPress={() => {
navigation.goBack();
}}
/>
),
headerRight: (
<ButtonWithoutBackground
buttonText={styles.buttonText}
onPress={() => params.referencedSharePost()}
>
Post
</ButtonWithoutBackground>
)
};
};
我想在stackNavigationOptions的onPress()中使用这个.props.doShare 在将其导出为默认值后,您不需要单独连接
组件执行期间的redux状态,因为它没有实例
因此,您可以将第二个代码片段中的代码移动到SharePostScreen
并在那里使用它,以访问道具
const mapStateToProps = state => {
return {
sharedPost: state.mainFeed.updatedPost
};
};
const mapDispatchToProps = dispatch => {
return {
doShare: bindActionCreators(doShare, dispatch)
};
};
export default connect(mapStateToProps, mapDispatchToProps)(SharePostScreen);
您可以添加代码来说明如何将redux道具绑定到屏幕上,以及您需要在哪里访问它吗?我添加了额外的代码Pritish。请尝试doShare:(params)=>console.log(params)
并在this.props.doShare('Test)中传递一些内容代码>,其余的代码似乎是正确的。我试过了。没用。你能说得更具体一点吗?又工作了。非常感谢兄弟。
const mapStateToProps = state => {
return {
sharedPost: state.mainFeed.updatedPost
};
};
const mapDispatchToProps = dispatch => {
return {
doShare: bindActionCreators(doShare, dispatch)
};
};
export default connect(mapStateToProps, mapDispatchToProps)(SharePostScreen);