如何在 StackNavigator 中将参数传递给屏幕?
How to Pass Parameters to screen in StackNavigator?
我的 React Native 代码:
import React, { Component } from 'react';
import { AppRegistry, ActivityIndicator, StyleSheet, ListView,
Text, Button, TouchableHighlight, View } from 'react-native';
import { StackNavigator } from 'react-navigation';
import DetailsPage from './src/screens/DetailsPage';
class HomeScreen extends React.Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
userDataSource: ds,
};
}
componentDidMount(){
this.fetchUsers();
}
fetchUsers(){
fetch('https://jsonplaceholder.typicode.com/users')
.then((response) => response.json())
.then((response) => {
this.setState({
userDataSource: this.state.userDataSource.cloneWithRows(response)
});
});
}
onPress(user){
this.props.navigator.push({
id: 'DetailPage'
});
}
renderRow(user, sectionID, rowID, highlightRow){
return(
<TouchableHighlight onPress={()=>{this.onPress(user)} } >
<View style={styles.row}>
<Text style={styles.rowText}> {user.name} </Text>
</View>
</TouchableHighlight>
)
}
render(){
return(
<ListView
dataSource = {this.state.userDataSource}
renderRow = {this.renderRow.bind(this)}
/>
)
}
}
导航配置:
const NavigationTest = StackNavigator({
Home: { screen: HomeScreen },
DetailsPage: { screen: DetailsPage },
});
详细信息屏幕是:
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import styles from '../styles';
export default class DetailsPage extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: `User: ${navigation.state.params.user.name}`,
});
render() {
const { params } = this.props.navigation.state;
return (
<View>
<Text style={styles.myStyle}>Name: {params.name}</Text>
<Text style={styles.myStyle}>Email: {params.email}</Text>
</View>
);
}
}
我无法使用以下代码将 user
传递给 DetailsPage
:
onPress(user){
this.props.navigator.push({
id: 'DetailPage'
});
}
我想使用 onPress
函数导航到 DetailPage
。如果我这样提醒它:
onPress(user){ Alert.alert(user.name)}
我确实得到了这个值,但我如何将它传递到另一个页面?
非常感谢!
您可以使用 navigate
函数的第二个参数传递参数:
onPress(user) {
this.props.navigation.navigate(
'DetailPage',
{ user },
);
}
React 导航 5.x (2020)
在 this.props.route.params
中访问它们。例如在您的 DetailsPage
中:
<Text style={styles.myStyle}>{this.props.route.params.user.name}</Text>
https://reactnavigation.org/docs/params/
React 导航 <= 4.x
在 this.props.navigation.state.params
中访问它们。例如在你的 DetailsPage
:
<Text style={styles.myStyle}>{this.props.navigation.state.params.user.name}</Text>
在 React 挂钩中,参数使用 useNavigation 在导航中发送
import { useNavigation } from '@react-navigation/native';
const navigation = useNavigation();
<Button
title="Back"
onPress={() => {
navigation.navigate('MyScreen',{name:'test'});
}}
/>
然后使用 useNavigationParam
访问它们
function MyScreen() {
const name = useNavigationParam('name');
return <p>name is {name}</p>;
}
See this .
它解决了我的问题。
this.props.navigation.navigate("**stack_Name**", {
screen:"screen_name_connect_with_**stack_name**",
params:{
user:"anything_string_or_object"
}
})
在你的第一页,说国家列表
const CountriesList = ({ navigation }) => {
/* Function to navigate to 2nd screen */
const viewCountry = (country) => {
navigation.navigate('ListItemPageRoute', { name: country.country_name });
};
}
对于您的第二个页面名称,说 ListItemPageRoute
const ListItemPageRoute = (props) => {
return (
<View style={styles.container}>
<Text style={styles.countryText}> { props.route.params.name } </Text>
</View>
);
};
'Props.route' 对象将包含您想从一个屏幕传递到另一个屏幕的所有参数的列表。
对我来说,通过 useNavigation() 和 useRoute() 解决了它:
对于功能组件:
import * as React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';
function MyBackButton() {
const navigation = useNavigation();
return (
<Button
title="Back"
onPress={() => {
navigation.goBack();
}}
/>
);
}
对于 class 组件:
class MyText extends React.Component {
render() {
// Get it from props
const { route } = this.props;
}
}
// Wrap and export
export default function(props) {
const route = useRoute();
return <MyText {...props} route={route} />;
}
onPress={() => {this.props.navigation.navigate('AllImages', {类型: item.type })}
console.log('valuesstrong text', this.props.route.params.Types);**
1)如何通过参数将数据发送到另一个屏幕:
onPress={(item) => props.navigation.navigate('Your ScreenName', {item : item})}
2)如何在另一个屏幕上获取数据:
const {item} = props.route.params
你可以轻松做到。
你想传递一些参数到你的屏幕?这样做:
export default () => {
const MainScreenComponent = ({navigation}) => (
<MainScreen
navigation={navigation}
/**
*
* And here your parameters...
*
*/
/>
);
const HomeScreenComponent = ({navigation}) => (
<HomeScreen
navigation={navigation}
/**
*
* And here your parameters...
*
*/
/>
);
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="MainScreen">
<Stack.Screen name="MainScreen" component={MainScreenComponent}/>
<Stack.Screen name="HomeScreen" component={MomeScreenComponent}/>
</Stack.Navigator>
</NavigationContainer>
);
};
我的 React Native 代码:
import React, { Component } from 'react';
import { AppRegistry, ActivityIndicator, StyleSheet, ListView,
Text, Button, TouchableHighlight, View } from 'react-native';
import { StackNavigator } from 'react-navigation';
import DetailsPage from './src/screens/DetailsPage';
class HomeScreen extends React.Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
userDataSource: ds,
};
}
componentDidMount(){
this.fetchUsers();
}
fetchUsers(){
fetch('https://jsonplaceholder.typicode.com/users')
.then((response) => response.json())
.then((response) => {
this.setState({
userDataSource: this.state.userDataSource.cloneWithRows(response)
});
});
}
onPress(user){
this.props.navigator.push({
id: 'DetailPage'
});
}
renderRow(user, sectionID, rowID, highlightRow){
return(
<TouchableHighlight onPress={()=>{this.onPress(user)} } >
<View style={styles.row}>
<Text style={styles.rowText}> {user.name} </Text>
</View>
</TouchableHighlight>
)
}
render(){
return(
<ListView
dataSource = {this.state.userDataSource}
renderRow = {this.renderRow.bind(this)}
/>
)
}
}
导航配置:
const NavigationTest = StackNavigator({
Home: { screen: HomeScreen },
DetailsPage: { screen: DetailsPage },
});
详细信息屏幕是:
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import styles from '../styles';
export default class DetailsPage extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: `User: ${navigation.state.params.user.name}`,
});
render() {
const { params } = this.props.navigation.state;
return (
<View>
<Text style={styles.myStyle}>Name: {params.name}</Text>
<Text style={styles.myStyle}>Email: {params.email}</Text>
</View>
);
}
}
我无法使用以下代码将 user
传递给 DetailsPage
:
onPress(user){
this.props.navigator.push({
id: 'DetailPage'
});
}
我想使用 onPress
函数导航到 DetailPage
。如果我这样提醒它:
onPress(user){ Alert.alert(user.name)}
我确实得到了这个值,但我如何将它传递到另一个页面?
非常感谢!
您可以使用 navigate
函数的第二个参数传递参数:
onPress(user) {
this.props.navigation.navigate(
'DetailPage',
{ user },
);
}
React 导航 5.x (2020)
在 this.props.route.params
中访问它们。例如在您的 DetailsPage
中:
<Text style={styles.myStyle}>{this.props.route.params.user.name}</Text>
https://reactnavigation.org/docs/params/
React 导航 <= 4.x
在 this.props.navigation.state.params
中访问它们。例如在你的 DetailsPage
:
<Text style={styles.myStyle}>{this.props.navigation.state.params.user.name}</Text>
在 React 挂钩中,参数使用 useNavigation 在导航中发送
import { useNavigation } from '@react-navigation/native';
const navigation = useNavigation();
<Button
title="Back"
onPress={() => {
navigation.navigate('MyScreen',{name:'test'});
}}
/>
然后使用 useNavigationParam
访问它们function MyScreen() {
const name = useNavigationParam('name');
return <p>name is {name}</p>;
}
See this .
它解决了我的问题。
this.props.navigation.navigate("**stack_Name**", {
screen:"screen_name_connect_with_**stack_name**",
params:{
user:"anything_string_or_object"
}
})
在你的第一页,说国家列表
const CountriesList = ({ navigation }) => {
/* Function to navigate to 2nd screen */
const viewCountry = (country) => {
navigation.navigate('ListItemPageRoute', { name: country.country_name });
};
}
对于您的第二个页面名称,说 ListItemPageRoute
const ListItemPageRoute = (props) => {
return (
<View style={styles.container}>
<Text style={styles.countryText}> { props.route.params.name } </Text>
</View>
);
};
'Props.route' 对象将包含您想从一个屏幕传递到另一个屏幕的所有参数的列表。
对我来说,通过 useNavigation() 和 useRoute() 解决了它:
对于功能组件:
import * as React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';
function MyBackButton() {
const navigation = useNavigation();
return (
<Button
title="Back"
onPress={() => {
navigation.goBack();
}}
/>
);
}
对于 class 组件:
class MyText extends React.Component {
render() {
// Get it from props
const { route } = this.props;
}
}
// Wrap and export
export default function(props) {
const route = useRoute();
return <MyText {...props} route={route} />;
}
onPress={() => {this.props.navigation.navigate('AllImages', {类型: item.type })} console.log('valuesstrong text', this.props.route.params.Types);**
1)如何通过参数将数据发送到另一个屏幕: onPress={(item) => props.navigation.navigate('Your ScreenName', {item : item})}
2)如何在另一个屏幕上获取数据: const {item} = props.route.params
你可以轻松做到。
你想传递一些参数到你的屏幕?这样做:
export default () => {
const MainScreenComponent = ({navigation}) => (
<MainScreen
navigation={navigation}
/**
*
* And here your parameters...
*
*/
/>
);
const HomeScreenComponent = ({navigation}) => (
<HomeScreen
navigation={navigation}
/**
*
* And here your parameters...
*
*/
/>
);
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="MainScreen">
<Stack.Screen name="MainScreen" component={MainScreenComponent}/>
<Stack.Screen name="HomeScreen" component={MomeScreenComponent}/>
</Stack.Navigator>
</NavigationContainer>
);
};