Som*_*ame 27 react-native react-navigation
我的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)}
/>
)
}
}
Run Code Online (Sandbox Code Playgroud)
导航配置:
const NavigationTest = StackNavigator({
Home: { screen: HomeScreen },
DetailsPage: { screen: DetailsPage },
});
Run Code Online (Sandbox Code Playgroud)
详细信息屏幕是:
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法通过代码传递user给DetailsPage:
onPress(user){
this.props.navigator.push({
id: 'DetailPage'
});
}
Run Code Online (Sandbox Code Playgroud)
我想导航到DetailPage与onPress功能.如果我提醒它:
onPress(user){ Alert.alert(user.name)}
Run Code Online (Sandbox Code Playgroud)
我确实得到了价值,但我如何将其传递给其他页面?
非常感谢!
jjj*_*jjj 57
你可以使用navigate函数的第二个参数传递params :
onPress(user) {
this.props.navigation.navigate(
'DetailPage',
{ user },
);
}
Run Code Online (Sandbox Code Playgroud)
然后访问它们this.props.navigation.state.params.例如在你的DetailsPage:
<Text style={styles.myStyle}>{this.props.navigation.state.params.user.name}</Text>
Run Code Online (Sandbox Code Playgroud)
参考:https://reactnavigation.org/docs/params.html
在反应钩子中,参数使用 useNavigation 发送导航
import { useNavigation } from '@react-navigation/native';
const navigation = useNavigation();
<Button
title="Back"
onPress={() => {
navigation.navigate('MyScreen',{name:'test'});
}}
/>
Run Code Online (Sandbox Code Playgroud)
然后使用 useNavigationParam 访问它们
function MyScreen() {
const name = useNavigationParam('name');
return <p>name is {name}</p>;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
看到这个。
它解决了我的问题。
this.props.navigation.navigate("**stack_Name**", {
screen:"screen_name_connect_with_**stack_name**",
params:{
user:"anything_string_or_object"
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48052 次 |
| 最近记录: |