wdl*_*x11 13 javascript android ios reactjs react-native
我试图在我的应用程序中的屏幕之间传递数据.目前我正在使用
"react-native": "0.46.0",
"react-navigation": "^1.0.0-beta.11"
Run Code Online (Sandbox Code Playgroud)
我有我的index.ios
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);
Run Code Online (Sandbox Code Playgroud)
应用为
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
Run Code Online (Sandbox Code Playgroud)
然后秒屏
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
Run Code Online (Sandbox Code Playgroud)
每当我调试console.log时,我都会被定义.
https://reactnavigation.org/docs/navigators/navigation-prop
文档说每个屏幕应该有这些值我做错了什么?
Ped*_*lin 16
所有其他答案现在似乎已经过时。在当前的 React 导航版本 ( "@react-navigation/native": "^5.0.8",) 中,您首先在一个屏幕之间传递值,如下所示:
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
/* 1. Navigate to the Details route with params, passing the params as an object in the method navigate */
navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
然后在您正在重定向的组件中,您将获得这样传递的数据:
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>Details Screen</Text>
<Text>itemId: {JSON.stringify(itemId)}</Text>
<Text>otherParam: {JSON.stringify(otherParam)}</Text>
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
所以,基本上,数据现在在里面this.props.route.params。在上面的那些例子中,我展示了如何从功能组件中获取它们,但在类组件中是类似的,我做了这样的事情:
首先我从这个 ProfileButton 传递数据,在它的handleNavigate函数中,像这样:
// these ProfileButton and ProfileButtonText, are a Button and a Text, respectively,
// they were just styled with styled-components
<ProfileButton
onPress={() => this.handleNavigate(item)
<ProfileButtonText>
check profile
</ProfileButtonText>
</ProfileButton>
Run Code Online (Sandbox Code Playgroud)
哪里handleNavigate是这样的:
handleNavigate = user => {
// the same way that the data is passed in props.route,
// the navigation and it's method to navigate is passed in the props.
const {navigation} = this.props;
navigation.navigate('User', {user});
};
Run Code Online (Sandbox Code Playgroud)
然后,函数 HandleNavigate 重定向到用户页面,这是一个类组件,我得到这样的数据:
import React, {Component} from 'react';
import {View, Text} from 'react-native';
export default class User extends Component {
state = {
title: this.props.route.params.user.name,
};
render() {
const {title} = this.state;
return (
<View>
<Text>{title}</Text>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在类组件中,我发现的方法是使这条线很长, title: this.props.route.params.user.name,但它有效。如果有人知道如何在当前版本的 react-native 导航中缩短它,请赐教。我希望这能解决你的问题。
Pop*_*ack 15
在你的代码中,props.navigation和this.props.navigation.state是两个不同的东西.您应该在第二个屏幕中尝试这个:
const {state} = props.navigation;
console.log("PROPS " + state.params.user);
Run Code Online (Sandbox Code Playgroud)
这const {state}条线只是为了获得易于阅读的代码.
小智 9
头等舱
<Button onPress = {
() => navigate("ScreenName", {name:'Jane'})
} />
Run Code Online (Sandbox Code Playgroud)
二等
const {params} = this.props.navigation.state
Run Code Online (Sandbox Code Playgroud)
反应导航3. *
this.props.navigation.navigate('Child', {
something: 'Some Value',
});
Run Code Online (Sandbox Code Playgroud)
this.props.navigation.state.params.something // outputs "Some Value"
Run Code Online (Sandbox Code Playgroud)