React native 反应导航状态。参数不工作
它不起作用。我什么都试过了。什么都不管用<代码>状态。如果您制作高级应用程序,参数根本不存在 我的“导航”有这个问题。手册中说params对象应该在那里,但它不在那里 当我链接到屏幕2时,我在屏幕1中设置了一个React native 反应导航状态。参数不工作,react-native,react-navigation,fetch-api,React Native,React Navigation,Fetch Api,它不起作用。我什么都试过了。什么都不管用状态。如果您制作高级应用程序,参数根本不存在 我的“导航”有这个问题。手册中说params对象应该在那里,但它不在那里 当我链接到屏幕2时,我在屏幕1中设置了一个id参数: <TouchableWithoutFeedback onPress={ ()=> this.props.navigation.navigate('FontsTab', { id: item.id }) } style={styles.listHeader} >
id
参数:
<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>
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>
);
}
}
类字体扩展组件{
建造师(道具){
超级(道具);
此.state={
参数:null,
已选择索引:0,
数值:0.5,
数据源:null,
孤岛加载:正确
};
this.componentDidMount=this.componentDidMount.bind(this);
}
getNavigationParams(){
返回this.props.navigation.state.params | |{}
}
componentDidMount(){
返回获取('http://www.koolbusiness.com/newvi/4580715507220480.json')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
…这个州,
孤岛加载:false,
数据来源:responseJson,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
if(此.state.isLoading){
返回(
我还有这个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;
import React,{Component}来自'React';
从“react native”导入{View,Text,StyleSheet};
从“react navigation”导入{StackNavigator};
从“react native elements”导入{Icon};
从“../views/font_home”导入字体名称;
从“../views/font_detail”导入字体详细信息;
const FontsTabView=({navigation})=>(
);
const FontsDetailTabView=({navigation})=>(
);
const FontsTab=StackNavigator({
主页:{
屏幕:FontsTabView,
路径:“/”,
导航选项:({navigation})=>({
标题:“”,
左校长:(
navigation.navigate('drawerropen')}
/>
),
}),
},
详情:{
屏幕:FontsDetailTabView,
路径:“字体\详细信息”,
导航选项:{
标题:“字体详细信息”,
},
},
});
导出默认FontsTab;
这个.props.navigation.state.params.id将为您提供从screen1传递的参数id的值。我将屏幕放在正确的堆栈导航器中,然后它工作了。我也这么认为,但显然我做错了什么。当我调试这个.props.navigation
时,那里没有状态
对象!它说routeName:“Home”
当我调试状态
对象时。为什么routeName不是“FontsTab”?
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;