[英]Passing Data Using React-Native Navigation
Im trying to pass data between screens in my app.我试图在我的应用程序的屏幕之间传递数据。 Currently I am using
目前我正在使用
"react-native": "0.46.0",
"react-navigation": "^1.0.0-beta.11"
I have my index.js我有我的 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);
app as应用程序作为
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
then in the secondscreen.js where we will fetch the data which passed from the previous screen as然后在 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
Whenever I console.log I get undefined.每当我 console.log 我得到未定义。
https://reactnavigation.org/docs/navigators/navigation-prop The docs say every screen should have these values what am I doing wrong? https://reactnavigation.org/docs/navigators/navigation-prop文档说每个屏幕都应该有这些值我做错了什么?
In your code, props.navigation
and this.props.navigation.state
are two different things.在你的代码中,
props.navigation
和this.props.navigation.state
是两个不同的东西。 You should try this in your second screen:您应该在第二个屏幕中尝试此操作:
const {state} = props.navigation;
console.log("PROPS " + state.params.user);
the const {state}
line is only here to get an easy to read code. const {state}
行只是为了获得易于阅读的代码。
All the other answers now seem outdated.所有其他答案现在似乎已经过时。 In the current react navigation version, (
"@react-navigation/native": "^5.0.8",
), you first pass value between one screen from another like this:在当前的 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>
);
}
and then in the component you are redirecting, you get the data you passed like this:然后在您正在重定向的组件中,您将获得像这样传递的数据:
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>
);
}
So, basically, the data now is inside this.props.route.params
.所以,基本上,数据现在在
this.props.route.params
里面。 In those examples above, I showed how to get them from functional components, but in class components is similar, I did something like this:在上面的那些例子中,我展示了如何从功能组件中获取它们,但在类组件中是类似的,我做了这样的事情:
First I passed the data from this ProfileButton, within it's handleNavigate
function, like this:首先,我从这个 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>
where the handleNavigate
goes like this: 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});
};
Then, the function HandleNavigate redirects to the user page, which is a class component, and I get the data like this:然后,函数 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>
);
}
}
In class components, the way I found out is making this quite long line title: this.props.route.params.user.name,
but it works.在类组件中,我发现的方法是制作这个很长的行
title: this.props.route.params.user.name,
但它有效。 If anyone knows how to make it shorter in the current version of react-native navigation, please enlighten me.如果有人知道如何在当前版本的 react-native 导航中缩短它,请赐教。 I hope this solves your problem.
我希望这能解决你的问题。
First Class头等舱
<Button onPress = {
() => navigate("ScreenName", {name:'Jane'})
} />
Second Class二等舱
const {params} = this.props.navigation.state
react-navigation 3.*反应导航 3.*
this.props.navigation.navigate('Child', {
something: 'Some Value',
});
this.props.navigation.state.params.something // outputs "Some Value"
您可以使用相关组件(SecondScreen)中的props.navigation.state.params.user
访问您的参数user
。
From react navigaton 3.x docs , you can use getParam(params)
.从 react navigaton 3.x docs ,您可以使用
getParam(params)
。
class SecondScreen extends React.Component {
render() {
const { navigation } = this.props;
const fname = navigation.getParam('user');
return (
<View>
<Text>user: {JSON.stringify(fname)}</Text>
</View>
);
}
}
I have developed an NPM package for send data from one component to other components.我开发了一个 NPM 包,用于将数据从一个组件发送到其他组件。 Please do check and use its easy to use.
请检查并使用它易于使用。
import { DataNavigation } from 'react-data-navigation';
.
.
.
// For set the data you need to call setData(key, value) Function i.e.
// eg. DataNavigation.setData('name', 'Viren');
// it will set the 'Viren' as respect to 'name' key.
import { DataNavigation } from 'react-data-navigation';
.
.
.
// Here we want to get the name value, which you set in home component than
// console.log('Hey my name is' + DataNavigation.getData('name'));
// it will print in console : Hey my name is Viren.
Comment down for any help.评论任何帮助。
在 2021 年,您可以使用
this.props.route.params.data
Using Hooks使用钩子
Screen1.js
navigation.navigate('Screen2',{ user_name: 'aaa',room_id:'100' });
Screen2.js
export default function Screen2({navigation){
return(
<View>
<Text> {navigation.getParams('user_name')} </Text>
</View>
)
}
Function Component.功能组件。
First Screen第一个屏幕
How to go in the another screen and also pass data from ono screen to another.如何进入另一个屏幕并将数据从 ono 屏幕传递到另一个屏幕。
<TouchableOpacity onPress={()=>props.navigation.navigate("screenName",{name:'saurabh'})}>
</TouchableOpacity>
Second Screen第二屏
using the following ways we can get data from first screen in second screen.使用以下方式,我们可以从第二个屏幕中的第一个屏幕获取数据。
props.navigation.state.params.name
Es6: ES6:
const Data= props.navigation.state.params.name;
Component(secondScreen):组件(第二屏):
const SecondScreen=(props)=>{
const Data= props.navigation.state.params;
return(
<View>
<Text>{Data.name}</Text>
</View>
)
}
export default SecondScreen;
Try this尝试这个
navigation.navigate('RouteName', { props: info })
//on navigated screen
const xyz=({navigation, route})=>{
console.log(route.params.props)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.