通过屏幕堆栈传递道具

问题描述 投票:0回答:1

我想通过反应式原生中的屏幕栈元素来传递一些道具。我有一个按钮,在按下时将会使用react导航要求一个屏幕,像这样。

    <Button
      title="Lees Meer"
      color="#d10a10"
      onPress={() => RootNavigation.navigate('Article', {
        params: { title: title, text: text, image: image },
      })}
    />

我希望在文章中使用这些参数来填充文本、标题和图片。我想我可以像这样在文章中使用它们。

function ArticleFull({ navigation, params }) {
    return (
        <View>
            <Header/>
            <Card>
                <CardItem header bordered>
                    <Body>
                        <Image
                            style={{ width: '100%', height: 400 }}
                            source={{ uri: 'https://www.holland.com/upload_mm/9/a/b/68638_fullimage_zwolle_sassenpoort.jpg' }}
                        />
                        <Text>
                            {params.text}
                        </Text>
                    </Body>
                </CardItem>
            </Card>
            <Button
                title="Go Back"
                color="#d10a10"
                width= "10%"
                onPress={() => navigation.goBack()}
            />
        </View>
    );
}

export default ArticleFull; 

在app.js中,我做了这些屏幕栈,用来导航到文章,但我需要它包含一些参数,这些参数是在主页上用按钮设置的。

const Stack = createStackNavigator()。

App.js:

export default class App extends Component {

  render() {
    return (
      <NavigationContainer ref={navigationRef}>
        <Stack.Navigator initialRouteName="Home" >
          <Stack.Screen options={{headerShown: false}} name="Home" component={HomePage} />
          <Stack.Screen options={{headerShown: false}} name="Article" component={ArticleFull} />
        </Stack.Navigator>
      </NavigationContainer>
    );
  }
}
react-native react-navigation
1个回答
1
投票

你可以像这样传递:-

 <Button
    title="Go to Details"
    onPress={() => {
      /* 1. Navigate to the Details route with params */
      navigation.navigate('Details', {
        itemId: 86,
        otherParam: 'anything you want here',
      });
    }}
  />

并像这样接收:-

function DetailsScreen({ route, navigation }) {
 /* 2. Get the param */
 const { itemId } = route.params;
const { otherParam } = route.params;
 return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  <Text>itemId: {JSON.stringify(itemId)}</Text>
  <Text>otherParam: {JSON.stringify(otherParam)}</Text>
   );
  }

希望能帮到你!!!

© www.soinside.com 2019 - 2024. All rights reserved.