我正在将数组映射到组件。
{this.state.forms.map((data)=><Box data={data} />)}
组件Box的代码为
import React,{Component} from 'react'
import {View,Text,ScrollView,Image,Button,StyleSheet,AsyncStorage} from 'react-native'
class Login extends Component{
constructor(props){
super(props)
this.state = {
forms:[],
}
this.onSetCompany = this.onSetCompany.bind(this)
}
onSetCompany(data){
console.log(data)
// AsyncStorage.setItem('Company',data);
this.props.navigation.navigate('CompanyDeatails')
}
render(){
return(
<View style={style.container}>
{console.log(this.props.data)}
<View style={style.box}>
<View style={style.box1}>
<Text style={style.row2}>{this.props.data.CompanyName} bkb</Text>
</View>
<View style={style.box3}>
<Text style={style.row2}>{this.props.data.LastDate} </Text>
</View>
</View>
<View style={style.buttonBox}>
<View style={style.button}>
<Button color="#6d5d17" title="Fill" onPress={()=>this.props.navigation.navigate('CompanyDeatails')}></Button>
</View>
</View>
</View>
)
}
}
export default Login
var style = StyleSheet.create({
.....
})
和我的路线
export default class App extends Component{
render(){
return(
<Links />
)
}
}
const Links = DrawerNavigator({.....,
CompanyDeatails:{screen:CompanyDeatails},
....,
},{contentComponent:props => <Slider {...props}/>
})
当我将数组映射到Box时,该框中的导航无法正常工作。我的其余应用程序都可以正常运行,但在此特定组件中无法正常运行。
喜欢
在login.js中,我有this.props.navigation.navigate('C1')
这是可行的,但是当我在Box组件中粘贴同一行时会抛出错误。
无法读取未定义的属性导航
答案 0 :(得分:1)
如果Box
组件不是navigator
的一部分,则应将其包装在withNavigation
函数中,以便访问navigation
对象。看看文档here
考虑以下示例
import React from 'react';
import { withNavigation } from 'react-navigation';
class Box extends React.Component {
...
render() {
...
}
}
export default withNavigation(Box);
现在,在Box组件中,您可以访问this.props.navigation.navigate
功能。
希望这会有所帮助!