传递道具StackNavigator

Ans*_*ori 6 react-native react-navigation

我尝试在stacknavigator上传递道具,这是我的代码

const MainCart = StackNavigator({
  Cart: {
    screen: CartScreen
  },
  Checkout: {
    screen: COScreen
  }

  /* how to pass 'myprops' in this area? */

});


export default class ReactNative_RefreshControl extends Component {

  constructor(props) {
    super(props)
  }

  render() {
    console.log('ReactNative_RefreshControl this.props.myparam : ' + this.props.myparam);
    return <MainCart myprops = {
      this.props.myparam
    }
    />;

    //https://reactnavigation.org/docs/navigators/stack#Navigator-Props
  }


}
Run Code Online (Sandbox Code Playgroud)

如何在StackNavigator区域传递'myprops'?

谢谢

Ash*_*lal 13

如果你想在那个地方传递道具你就像这样使用它.

const MainCart = StackNavigator({
 Cart: {
    screen: props=> <CartScreen {...props} screenProps={other required prop}>
 },
 Checkout: {
    screen: COScreen
 }
Run Code Online (Sandbox Code Playgroud)

如果你想在Sagar Chavada导航解决方案时传递道具很有用

  • @shan 我已经添加了另一种方法的示例,但推荐使用 React Context 库。 (3认同)

小智 10

对于那些正在寻找 React Navigation 5 的人

<Stack.Navigator initialRouteName="LoginScreen" headerMode="none">
 <Stack.Screen name="LoginScreen" component={LoginScreen} initialParams={{'key':'value'}} />
 <Stack.Screen name="CodeVerificationScreen" component={CodeVerificationScreen} initialParams={{'key':'value'}} />
</Stack.Navigator>
Run Code Online (Sandbox Code Playgroud)

您可以在 login.js 中接收初始参数

console.log(this.props.route.params.key)
Run Code Online (Sandbox Code Playgroud)


Sag*_*ada 5

在您的点击事件中,请执行以下操作:

    onPressed(movie){
        this.props.navigation.navigate(
          'screen',
          {movie: movie}
        });
      }

<TouchableHighlight onPress={() => this.onPressed(movie)}>
Run Code Online (Sandbox Code Playgroud)

在其他屏幕上,您可以这样设置:

export default class NavigateTo extends Component {
  constructor(props){
    super(props);

    this.state = {
        name: this.props.navigation.state.params.movie.title,
        year: this.props.navigation.state.params.movie.year
    }
  }
Run Code Online (Sandbox Code Playgroud)

movie是一个json对象,其中包含标题,年份,发行日期,收入,海报等。

这是用于旧的导航:

onPressed(movie){
    this.props.navigator.push({
      id:'page2',
      movie: movie
    });
  }
Run Code Online (Sandbox Code Playgroud)

在第二个屏幕中:

this.state = {
        name: this.props.movie.title,
        email: this.props.movie.year
    }
Run Code Online (Sandbox Code Playgroud)

现在你可以看到区别

如果这不起作用,那么尝试这样

<Button title="Second" onPress={() => navigate("MainCart",{},
  {
    type: "Navigate",
    routeName: "Checkout",
    params: {name:"Jo"}
  }
)} />
Run Code Online (Sandbox Code Playgroud)

检查此以获取嵌套堆栈导航 https://github.com/react-community/react-navigation/issues/143