react-native - 从 stack-navigator-options 使用 props 调用 redux 操作
问题描述
我想从堆栈导航器选项中的 redux 调用一个操作。这是我的代码。当我使用 this.props.action_name 使用动作时,它说这不是一个函数。我在这里做错了什么?我使用navigation.params 来实现这一点,但它仍然不起作用。
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>
)
};
};
这就是我将状态映射到道具的方式。
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import SharePostScreen from "./SharePostScreen";
import { doShare } from "../../config/actions";
const mapStateToProps = state => {
return {
sharedPost: state.mainFeed.updatedPost
};
};
const mapDispatchToProps = dispatch => {
return {
doShare: bindActionCreators(doShare, dispatch)
};
};
export default connect(mapStateToProps, mapDispatchToProps)(SharePostScreen);
我想在 stackNavigationOptions 的 onPress() 中使用 this.props.doShare。
解决方案
在将其导出为默认值后,您不需要单独进行操作connect
SharePostScreen
,因为在组件执行期间它没有 redux 状态的实例。
因此,您可以将第二个片段中的代码移动到SharePostScreen
并在那里使用它,以访问props
.
const mapStateToProps = state => {
return {
sharedPost: state.mainFeed.updatedPost
};
};
const mapDispatchToProps = dispatch => {
return {
doShare: bindActionCreators(doShare, dispatch)
};
};
export default connect(mapStateToProps, mapDispatchToProps)(SharePostScreen);
推荐阅读
- ruby-on-rails - 密码确认后测试不删除用户
- python - 'tf.data()' throwing 你的输入数据用完了;中断训练
- google-sheets - 如果 b6 等于 A,则从 H3 中减去 randbetween(1,10),否则返回“oof”。给出解析错误
- amazon-web-services - 将源代码从管道发送到 cloudformation 中的另一个模板
- el - 如何查看 oozie 和表达式语言版本?
- merge - 注意:SAS prep guide 数据合并问题有勘误表
- c++ - 使用 GenTL.h 的问题:在 GenIcam 包中找不到相应的库
- python - 模型已经包含一个属性“触发器”。跳过绑定
- linux - 如何将特殊字符串传递给 sed
- swagger - Open API 3.0.0:引用的文件无法访问父组件