gpt4 book ai didi

reactjs - 使用渲染外部的堆栈导航访问 channel 数据

转载 作者:行者123 更新时间:2023-12-02 12:09:27 25 4
gpt4 key购买 nike

我能够获取一个ListView并显示任何列表项的详细信息页面onPress。我可以在 DetailsPage 中显示单击项目的详细信息,但只能在 render() 中显示。如何访问渲染之外的任何值?我想使用该值从另一个 API 中获取信息

主页:

import React, { Component } from 'react';
import {
AppRegistry, StyleSheet, ListView,
Text, TouchableHighlight, View
} from 'react-native';

import { StackNavigator } from 'react-navigation';
import DetailsPage from './src/DetailsPage';

class HomeScreen extends React.Component {
static navigationOptions = {
title: 'MyApp!',
};

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)
});
});
}

renderRow(user, sectionID, rowID, highlightRow){
const { navigate } = this.props.navigation;
return(

<TouchableHighlight onPress={() => navigate('DetailsPage', {users: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 },
});



AppRegistry.registerComponent('NavigationTest', () => NavigationTest);

详细信息页面:

import React, { Component } from 'react';
import { StyleSheet, ListView, Text, TouchableHighlight, View } from 'react-native';

export default class DetailsPage extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: `${navigation.state.params.users.name}`,

});

// var userid = 2; --> This doesn't work as it returns Unexpected Token where var is mentioned.

constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
albumDataSource: ds,
};
}

componentDidMount(){
this.fetchAlbums();
}

fetchAlbums(){

var theUser = 2; //This is the value I want to receive from the clicked user.

// var newUser = this.props.navigation.state.users.id; -> this doesnt work.


fetch('https://jsonplaceholder.typicode.com/albums?userId='+newUser)
.then((response) => response.json())
.then((response) => {
this.setState({
albumDataSource: this.state.albumDataSource.cloneWithRows(response)
});
});
}


renderRow(album, sectionID, rowID, highlightRow){

return(

<TouchableHighlight>
<View style={styles.row}>
<Text style={styles.rowText}> {album.userId} - {album.title} </Text>
</View>
</TouchableHighlight>
)
}


render() {
const { params } = this.props.navigation.state;
return (
<View style={styles.container}>
<Text style={styles.textstyle}>Screen Chat with {params.users.name}</Text>
<Text style={styles.textstyle}>Username : {params.users.username}</Text>
<Text style={styles.textstyle}>Email : {params.users.email}</Text>
<Text style={styles.textstyle}>ID : {params.users.id}</Text>

<ListView
dataSource = {this.state.albumDataSource}
renderRow = {this.renderRow.bind(this)}
/>
</View>
);
}
}

因此,我想使用 users.idDetailsPage 上获取更多数据并显示该数据。我怎么做。请帮忙。非常感谢。

最佳答案

这是我发现的,这对我来说看起来是错误的,

var newUser = this.props.navigation.state.users.id;

使用这个也许可以帮助你

console.log(this.props.naviagtion.state.params) **Check if are getting those value then use below one **
var newUser = this.props.navigation.state.params.users.id;

关于reactjs - 使用渲染外部的堆栈导航访问 channel 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45412351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com