加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React Navigation丢失传递下来的道具

发布时间:2020-12-15 05:04:35 所属栏目:百科 来源:网络整理
导读:我有一个连接到redux的容器,由于connect(mapStateToProps,mapDispatchToProps)行,它继承了’全局状态’作为道具… 容器(Container.js)依次将props传递给Table(Table.js),它按预期接收它们. 当我尝试使表成为导航堆栈时,我的问题出现了.原因是我希望能够点击
我有一个连接到redux的容器,由于connect(mapStateToProps,mapDispatchToProps)行,它继承了’全局状态’作为道具…

容器(Container.js)依次将props传递给Table(Table.js),它按预期接收它们.

当我尝试使表成为导航堆栈时,我的问题出现了.原因是我希望能够点击每一行,这些行将导航到DetailPage,这将由依赖于行的不同道具填充.

然而,当添加StackNavigator时,从Container传下来的所有道具都消失了,只有React Navigation的那些(导航:{actions:{goBack … ETC}}).

注意:如果我直接连接Component,也会发生这种情况.

下面是我的代码结构的dummied down示例.

我做错了什么/我如何保持传下来的道具?

Container.js

class Container extends Component {
  render() {
    return (
      <View>
        <Table {...this.props} />
      </View>
    )
  }
}


const mapStateToProps = state => {
  return {
    data: state.data,propTwo: state.propTwo
  }
}

const mapDispatchToProps = dispatch => {
  return {
    functionOne: () => dispatch(functionOne()),functionTwo: arg => dispatch(functionTwo(arg))
  }
}

export default connect(mapStateToProps,mapDispatchToProps)(Market);

Table.js

const DetailPage = () => <View><Text>Just a mock page</Text></View>

class Table extends Component {
  keyExtractor = (item,index) => index;
  renderItem = (argOne,index) => {
    return (
      <ListItem
        onPress={() => this.props.navigate(argOne)}
        key={index} />
    )
  }
  render() {
    return (
      <View>
        <FlatList
          keyExtractor={this.keyExtractor}
          data={this.props.data}
          renderItem={this.renderItem}
          refreshControl={
            <RefreshControl
              onRefresh={this.props.functionTwo}
            />
          }
        />
      </View>
    ) 
  }
}
// react-navigation StackNavigator
export default Table = StackNavigator({
  Table: { screen: Table },DetailPage: { screen: DetailPage }
});
您可以将StackNavigator移动到Container.js中:
export default StackNavigator({
  Table: { screen: connect(mapStateToProps,mapDispatchToProps)(Container) },DetailPage: { screen: DetailPage }
});

您也可以直接连接到Table,而不是创建Container包装类:

connect(mapStateToProps,mapDispatchToProps)(Table)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读