Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 使用道具从堆栈导航器选项调用redux操作_React Native - Fatal编程技术网

React native 使用道具从堆栈导航器选项调用redux操作

React native 使用道具从堆栈导航器选项调用redux操作,react-native,React Native,我想从堆栈导航器选项中的redux调用一个操作。这是我的密码。当我使用this.props.action_名称使用动作时,它表示这不是一个函数。我做错了什么?我使用navigation.params实现了这一点,但仍然不起作用 componentDidMount() { this.props.navigation.setParams({ referencedSharePost: this.sharePost, referencedDoShare: this.pro

我想从堆栈导航器选项中的redux调用一个操作。这是我的密码。当我使用this.props.action_名称使用动作时,它表示这不是一个函数。我做错了什么?我使用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>
      )
    };
  };

我想在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);