如何在React Native中将新参数传递到先前的屏幕?

时间:2020-02-29 14:49:02

标签: react-native react-native-navigation

当用户单击电影时,我有电影的平面列表,它将参数传递给电影详细信息屏幕,并显示电影详细信息,然后当用户选择其中任何一个时,该屏幕具有类似电影的平面列表,基本上应该将新参数传递给电影详细信息屏幕,但没有,它就像我在点击“后退”按钮一样....

onPress={() =>
              this.props.navigation.navigate('MovieDetails', {
                selectedItem: item,
              })
            }   // Passing params to movie details screen


console.log(this.props.navigation.getParam('selectedItem')) //receiving params in movie details screen

1 个答案:

答案 0 :(得分:0)

请检查以下示例。希望对您有帮助。

import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text style={{ fontSize: 20, marginBottom: 10 }}>Home Screen</Text>
        <Button
          title="Go to Details"
          onPress={() =>
            this.props.navigation.navigate('Details', {
              name: 'this params from homescreen',
            })
          }
        />
      </View>
    );
  }
}

class DetailsScreen extends React.Component {
  render() {
    console.log('screen', this.props.navigation.state);

    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text style={{ fontSize: 20, marginBottom: 10 }}>
          {this.props.navigation.state.params.name}
        </Text>
        <Button
          title="Go back"
          onPress={() => this.props.navigation.goBack()}
        />
      </View>
    );
  }
}

const RootStack = createStackNavigator({
  Home: HomeScreen,
  Details: DetailsScreen,
});

export default createAppContainer(RootStack);


有任何疑问。