【问题标题】:TypeError: undefined is not an object (evaluating '_this3props.navigation.state.params')TypeError:未定义不是一个对象(评估'_this3props.navigation.state.params')
【发布时间】:2020-04-23 00:52:07
【问题描述】:

我正在尝试使用 react-native expo 构建一个聊天应用程序,我在收件箱页面中收到此错误我还放置了我的聊天页面(这是我想要导航的内容) 请帮我! 我也把我的仪表板屏幕 我没有 route.js 文件。

"TypeError: undefined is not an object (evalating '_this3props.navigation.state.params')"

        export default class  GelenKutusu extends React.Component {
            state={
                userList:[]
            }
            componentWillMount(){
                firebase.database().ref().child('users').once('value',(snap)=>{
                    let userList=[]
                    snap.forEach((user)=>{
                        const {first_name,profile_picture,uid}=user.val()
                        userList.push({first_name,profile_picture,uid})
                })
                this.setState({userList})
            })
        }
            render() {
              return (
                              <View style={styles.container}>
                        <View style={{alignItems:'center',justifyContent:'center',width:width-40,paddingBottom:10}}>
                            <Text style={{color:'grey',fontWeight:'bold',fontSize:18}}>Inbox</Text>
                        </View>
                        <FlatList
                        data={this.state.userList}
                        keyExtractor={(item,index)=>item.first_name}
                        renderItem={({item})=>
                        <TouchableOpacity onPress={()=> this.props.navigation.navigate('Chat',{user:this.props.navigation.state.params.user, profile:item})} >
                            <View style={{flex:1,backgroundColor:'transparent', flexDirection: 'row', padding:5, width:width}}>
                           <Image style={{height:40, width:40,borderRadius:20}}
                              source={{ uri: item.profile_picture  }} />
                              <View style={{alignItems:'center',justifyContent:'center'}}>
                                 <Text style={{color:'grey', fontWeight:'bold'}}>{item.first_name}</Text>
                              </View>
                              </View>
                              <View style={{width:width, height:1,backgroundColor:'darkgrey'}} />
                              </TouchableOpacity>
                        } />
                    </View>
                );
            }
        } 


Here is chat.js


    export default class chat extends Component{
        state={
            messages:[],
            user:this.props.navigation.state.params.user,
            profile:this.props.navigation.state.params.profile
        }
        componentWillMount(){
            const {user,profile}=this.state
            this.chatID= user.uid > profile.uid? user.uid+'-'+profile.uid:profile.uid+'-'+user.uid
            this.watchChat()
        }
        watchChat=()=>{        firebase.database().ref('messages').child(this.chatID).on('value',snap=>{
                let messages=[]
                snap.forEach(messages=>{
                    messages.push(messages.val())
                })
                messages.reverse()
                this.setState({messages})
            })
        }
        onSend=(messages)=>{
            const {user,profile}=this.state
            relationMessage={}    firebase.database().ref('messages').child(this.chatID).push({...message[0],createdAt:new Date().getTime(),
            })
        }
        render(){
            const {user,profile}=this.state
            const avatar = user.profile_picture
            return(
                <View style={{flex:1}}>
                  <GiftedChat
                    messages={this.state.messages}
                    user={{_id:user.uid,avatar}}
                    onSend={this.onSend()}
                    />
                    </View>
            )
        }
    }




Here is my Dashboard.js screen 


class  DashboardScreen extends Component {

    render(){
        return (

          <NavigationContainer> 

          <Stack.Navigator
         screenOptions={{
           gestureEnabled: true,
           gestureDirection: "horizontal",

           // transitionSpec: {
             // open: config,
             // close: closeConfig
             // }
         }}
         headerMode="float"
         animmation="fade" >
           <Stack.Screen options={{
          headerRight: () => (

            <Ionicons name="ios-search" size={38}  
            style={{ margin: 5, padding: 5}}
            onPress={() => alert('This is a button!')}
            />

          ),
          headerLeft: () => (
            <Ionicons name="ios-add" size={48}  
            style={{ margin: 5, padding: 5}}
            onPress={() => alert('This is a button!')}
            />
          ),
        }} name = "                 "
           component={MyTabs} />
           <Stack.Screen  name="Header" component={MyTabsTop}  />
         </Stack.Navigator>

            </NavigationContainer>

        );
    }
}

export default DashboardScreen;

const Tab = createBottomTabNavigator();
function MyTabs() {
  return (
    <Tab.Navigator
    screenOptions={({ route }) => ({
      tabBarIcon: ({ focused, color, size }) => {
        let iconName;

        if (route.name === 'Home') {
          iconName ='ios-home'
        } 
        else if (route.name === 'Bildirimler') {
          iconName ='ios-notifications'
        }
        else if (route.name === 'GelenKutusu') {
          iconName ='ios-chatboxes'
        }
        else if (route.name === 'Profil') {
          iconName ='ios-contact'
        }

        // You can return any component that you like here!
        return <Ionicons name={iconName} size={size} color={color} />
      }
    })} >
      <Tab.Screen name="Home" component={Home} />
      <Tab.Screen name="Bildirimler" component={Bildirimler} />
      <Tab.Screen name="GelenKutusu" component={GelenKutusu} />
      <Tab.Screen name="Profil" component={Profil} />
    </Tab.Navigator>
  );
}

const Stack = createStackNavigator();
function MyTabsTop() {
    return (
        <Tab.Navigator>
        <Tab.Screen
          name="Home"
          component={Profil}
          options={{ title: 'My home' }}
        />
      </Tab.Navigator>
    );
  }

这是上下文。

【问题讨论】:

  • 您在使用导航吗?如果是这样,请分享您的导航代码。

标签: react-native expo


【解决方案1】:

问题来了

onPress={()=> this.props.navigation.navigate('Chat',{user:this.props.navigation.state.params.user, profile:item})}

您正在传递从另一个组件获取的参数。

首先像这样在渲染中获取参数:

 render() {
    /Read the params from the navigation state */
    const { params } = this.props.navigation.state;
    const user= params ? params.user: null;

在这里您将获得用户参数并在导航中使用

       onPress={()=> this.props.navigation.navigate('Chat',{user:user, profile:item})}

希望这会有所帮助!

【讨论】:

  • 感谢您的帮助,但还是一样。
  • 如果您没有传递参数,您是否可以导航到聊天屏幕?您可以发布您的导航并从您传递参数的屏幕
  • 不,我无法浏览聊天屏幕。我现在编辑你可以看到我的聊天屏幕的问题。
  • 您还没有发布您的导航页面,我们需要一个您已定义路线的页面
  • 我没有定义路由可能是问题所在。我放了dashboard.js。这些是我定义的唯一路由。我没有自己构建的知识。我正在从另一个视频的某些视频登录部分中进行聊天。我无法自学,我的语言没有 react-native 源代码。
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2021-12-17
  • 2021-12-01
  • 2019-11-16
  • 2020-02-09
相关资源
最近更新 更多