如何将导航参数发送到 mapStatetoProps 方法

how to send navigations params to mapStatetoProps method

我想将导航参数放入 mapStatetoProps 方法

我尽量避免在我的导航中使用 redux 以保持简单,但如果唯一的方法是使用 redux 那么我会使用它。

这是我的代码

class GameScreen extends React.Component {
  static navigationOptions = ({navigation}) => ({
    title: navigation.state.params.id,
  });

  render() {
    return (
      <View style={{flex:1, backgroundColor: '#005662'}}>
        <Text>ID</Text>
        <Text>{this.props.game.id}</Text>
        <Text>TITLE</Text>
        <Text>{this.props.game.title}</Text>
        </View>
    );
  }
}

function mapStateToProps(state, props) {
  return {
    game: state.games.find(item => item.id === /* how to put value from navigation.state.params.id to here */)
  }
}

export default connect(mapStateToProps)(GameScreen);

您可以 integrate React NavigationRedux 并在您的商店中拥有当前导航状态。

然后您可以在 mapStateToProps.

中轻松访问您的状态中的数据
function mapStateToProps(state, props) {
  return {
    game: state.games.find(item => item.id === state.nav...
  }
}

只需使用您自己的道具props.navigation.state.params.id无需实施 redux

function mapStateToProps(state, props) {
  return {
    game: state.games.find(item => item.id === props.navigation.state.params.id)
  }
}

nico1510 求助。