我试图在我的应用程序的屏幕之间传递数据。目前我正在使用
"react-native": "0.46.0",
"react-navigation": "^1.0.0-beta.11"
我有我的 index.js
import React, { Component } from 'react';
import {
AppRegistry,
} from 'react-native';
import App from './src/App'
import { StackNavigator } from 'react-navigation';
import SecondScreen from './src/SecondScreen'
class med extends Component {
static navigationOptions = {
title: 'Home Screen',
};
render(){
const { navigation } = this.props;
return (
<App navigation={ navigation }/>
);
}
}
const SimpleApp = StackNavigator({
Home: { screen: med },
SecondScreen: { screen: SecondScreen, title: 'ss' },
});
AppRegistry.registerComponent('med', () => SimpleApp);
应用程序
import React, { Component } from 'react';
import {
StyleSheet,
Text,
Button,
View
} from 'react-native';
import { StackNavigator } from 'react-navigation';
const App = (props) => {
const { navigate } = props.navigation;
return (
<View>
<Text>
Welcome to React Native Navigation Sample!
</Text>
<Button
onPress={() => navigate('SecondScreen', { user: 'Lucy' })}
title="Go to Second Screen"
/>
</View>
);
}
export default App
然后在 secondscreen.js 中我们将获取从前一个屏幕传递的数据
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Button
} from 'react-native';
import { StackNavigator } from 'react-navigation';
const SecondScreen = (props) => {
const { state} = props.navigation;
console.log("PROPS" + state.params);
return (
<View>
<Text>
HI
</Text>
</View>
);
}
SecondScreen.navigationOptions = {
title: 'Second Screen Title',
};
export default SecondScreen
每当我使用 console.log 时,我都会变得不确定。
https://reactnavigation.org/docs/navigators/navigation-prop 文档说每个屏幕都应该有这些值我做错了什么?
原文由 wdlax11 发布,翻译遵循 CC BY-SA 4.0 许可协议
在您的代码中,
props.navigation
和this.props.navigation.state
是两个不同的东西。你应该在你的第二个屏幕上试试这个:const {state}
行只是为了获得易于阅读的代码。