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

标签 reactjs react-native fetch react-navigation

我能够获取一个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/

相关文章:

javascript - 如何在 react-helmet 中添加 javascript 代码片段?

javascript - 在为发布进行编译时,如何让React Native将JS bundle 放入Share Extension中?

javascript - 如何识别HandleErrorAttribute OnException中的javascript fetch()?

PHP/MySQL - 使用一个查询的结果在另一个查询中使用

php - 使用Codeigniter,程序无法正常运行

javascript:使用索引作为参数更新 Array() 中的属性值

javascript - react Redux : Unable to Map array of Objects

javascript - 将相同 Prop 和文本渲染到不同组件类型的 DRY React 方法

android - react native 获取。 CertPathValidatorException 错误

android - 在 React Native 中制作 slider 导航的最佳方法