react-native - react 导航 state.params 不起作用

标签 react-native react-navigation fetch-api

它不工作。我尝试了一切。什么都行不通。 state.params如果您制作高级应用程序,则根本不存在。

我对 react “导航”有这个问题。它在手册中说 params 对象应该在那里 https://reactnavigation.org/docs/navigation-prop.html#state-the-screen-s-current-state-route但事实并非如此。

我设置了一个 id当我链接到屏幕 2 时,屏幕 1 中的参数如下:

 <TouchableWithoutFeedback onPress={ ()=> this.props.navigation.navigate('FontsTab', { id: item.id }) } style={styles.listHeader} >
                <View  style={styles.listRowContainer}>
                    <View  style={styles.listinside1Container}>
                        <Image  style={styles.listImage} source={item.icon} />
                        <View  style={styles.listContainer} onPress={(event) => this._selectedItem(item.text)}  >
                            <Text style={styles.listHeader} >
                              {item.title}
                            </Text>
                            <Text  style={styles.listValue} >{item.value}</Text>
                            <Image 
                                style={{width: 50, height: 50}}
                                source={{uri: item.img}}
                            />

                        </View>
                    </View>

                                </View>
            </TouchableWithoutFeedback>

但它不起作用。在屏幕 2 中,我不能使用 state.params :
     <ScrollView style={styles.container}>
          <Text>{ JSON.stringify(this.props.navigation)}</Text>
          <Text>TEST{ state.params }</Text>
          <Image
              style={{width: 150, height: 150}}
              source={{uri: this.state.dataSource.img}}
          />
          <Text  style={styles.textStyle} >{this.state.dataSource.text}</Text>
      </ScrollView>
state.params只是什么都不返回。我该怎么办?

screen2 的完整类:
class Fonts extends Component {
    constructor(props) {
        super(props);
        this.state = {
            params: null,
            selectedIndex: 0,
            value: 0.5,
            dataSource: null,
            isLoading: true
        };
        this.componentDidMount = this.componentDidMount.bind(this);
    }
    getNavigationParams() {
        return this.props.navigation.state.params || {}
    }

    componentDidMount(){
        return fetch('http://www.koolbusiness.com/newvi/4580715507220480.json')
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    ...this.state,
                    isLoading: false,
                    dataSource: responseJson,
                }, function(){
                });
            })
            .catch((error) =>{
                console.error(error);
            });
    }
  render() {
      if(this.state.isLoading){
          return(
              <View style={{flex: 1, padding: 20}}>
                  <ActivityIndicator/>
              </View>
          )
      }
    return (
      <ScrollView style={styles.container}>
          <Text>{ JSON.stringify(this.props)}</Text>
          <Text>TEST{ this.state.params }</Text>
          <Image
              style={{width: 150, height: 150}}
              source={{uri: this.state.dataSource.img}}
          />
          <Text  style={styles.textStyle} >{this.state.dataSource.text}</Text>
      </ScrollView>
    );
  }
}

在我的应用程序中,可以通过 screen1 中的一个简单按钮重现这种痛苦:
<Button onPress={() => navigate('FontsTab', { name: 'Brent' })} title="Go to Brent's profile" />
然后切换到 FontsTab 工作但参数不在状态对象中:

enter image description here

我也有这个 tabview 的代码
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';

import { StackNavigator } from 'react-navigation';
import { Icon } from 'react-native-elements';

import FontsHome from '../views/fonts_home';
import FontsDetails from '../views/fonts_detail';

const FontsTabView = ({ navigation }) => (
  <FontsHome banner="Fonts" navigation={navigation} />
);

const FontsDetailTabView = ({ navigation }) => (
  <FontsDetails banner="Fonts Detail" navigation={navigation} />
);

const FontsTab = StackNavigator({
  Home: {
    screen: FontsTabView,
    path: '/',
    navigationOptions: ({ navigation }) => ({
      title: '',
      headerLeft: (
        <Icon
          name="menu"
          size={30}
          type="entypo"
          style={{ paddingLeft: 10 }}
          onPress={() => navigation.navigate('DrawerOpen')}
        />
      ),
    }),
  },
  Detail: {
    screen: FontsDetailTabView,
    path: 'fonts_detail',
    navigationOptions: {
      title: 'Fonts Detail',
    },
  },
});

export default FontsTab;

最佳答案

this.props.navigation.state.params.id将为您提供从 screen1 传递的 param id 的值。

关于react-native - react 导航 state.params 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49348242/

相关文章:

ios - 在 native Swiper 中动态呈现内容

javascript - Node 获取帮助 - 获取 FetchError : invalid json response > body at <url> reason: > Unexpected end of JSON input

ios - React-Native iOS 模拟器命令 + R 不工作

react-native - 将国家 View 添加到 react-native-maps

reactjs - 如何将图像从 JSON 文件放到 React Native Js 页面

react-native - 如何在 react 导航(3.x)中为全屏模式提供透明背景?

react-native - 当我使用 DrawerItemList 时,应用程序崩溃

reactjs - React Native 在 Nested StackNavigator 之间导航

javascript - net::ERR_EMPTY_RESPONSE && 未捕获( promise )类型错误:无法获取

node.js - 从经过身份验证的路由获取图像