相关文章推荐
刚失恋的蚂蚁  ·  React ...·  2 月前    · 
爽快的墨镜  ·  能否直接将React ...·  2 月前    · 
愉快的单车  ·  Web 嵌入 | Electron ...·  1 年前    · 
温柔的野马  ·  XMLHttpRequest.withCre ...·  2 年前    · 
飞奔的机器猫  ·  return new Promise ...·  2 年前    · 

(React Native)未定义不是一个对象(评估'navigation.navigate')。

0 人关注

在App.js中,我声明了 "FollowingScreen",它是由一个导出 "Following "的模块组成的。

export const FollowingScreen = ({route, navigation}) => {
return (
    <ScrollView style={styles.scrollView}>
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: "#262423" }}>
       <Following />
      </View>
    </ScrollView>

"关注 "是由一个叫 "following.js "的文件导出的。我想从 "following.js "导航到ProfileScreen。

import { useNavigation } from '@react-navigation/native';
class Following extends Component {
renderItem = ({item}, navigation) => (
<ListItem bottomDivider>
    <ListItem.Content style={{width: '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
    <ListItem.Title>{item.title}</ListItem.Title>
    <TouchableOpacity 
        onPress={() => navigation.navigate("ProfileScreen", {userInfo: {uri: item.probability, username: item.country_id, id_user: item.id_user}})} 
        style={{ flexDirection: 'row'}}
        <Image
        style={{ borderRadius: 50 }}
        source={{ uri: item.probability, width: 48, height: 48 }}
    </TouchableOpacity>
    <TouchableOpacity 
        onPress={() => navigation.navigate("ProfileScreen", {userInfo: {uri: item.probability, username: item.country_id, id_user: item.id_user}})} 
        style={{ flexDirection: 'row'}}
        <ListItem.Subtitle style={{color: '#000'}}>
        <Text style={{fontFamily: 'Montserrat_600SemiBold' }}>{item.name} {item.surname}</Text>
        {"\n"}
        <Text style={{fontFamily: 'Montserrat_600SemiBold' }}>@{item.country_id}</Text>
        {"\n"}
        {"\n"}
        <Text style={{fontFamily: 'Montserrat_600SemiBold' }}>{item.followers}</Text>
        {"\n"}
        <Text style={{fontFamily: 'Montserrat_600SemiBold' }}>{item.total}</Text> Total
        </ListItem.Subtitle>
    </TouchableOpacity>
    <Button
        buttonStyle={{backgroundColor: "#a6aba7", padding: 9, textAlign: "right", borderRadius: 10, display: item.isFollowing=="Same user" ? "none" : "flex"}}
        title={item.isFollowing}
        onPress={() => {
        if(item.isFollowing=="Follow"){
            this.follow(item.id_user);
        else if(item.isFollowing=="Unfollow"){
            this.unfollow(item.id_user);
        else if(item.isFollowing=="Same user"){
            //alert("Same user");
    </ListItem.Content>
</ListItem>

不幸的是,我得到 "undefined不是一个对象(评估'navigation.navigate')"

javascript
android
ios
reactjs
navigation
gigapico00
gigapico00
发布于 2022-10-04
2 个回答
Kailash
Kailash
发布于 2022-10-04
已采纳
0 人赞同

你没有将 navigation 作为一个道具传递给 Following 组件,所以同样地改变你的 FollowingScreen 组件。

export const FollowingScreen = ({route, navigation}) => {
  {/** Rest Of Code **/}
   <Following navigation={navigation}/>
  {/** Rest of Code **/}

Then in Following class use this.props.navigation.navigate(....).

trojan
trojan
发布于 2022-10-04
0 人赞同

你需要在调用它之前声明 "导航"。