Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant Violation tried to get frame out of range index NaN

I am running an react-native chat app example from Even Bacon and run into frame out of range NaN error:

enter image description here

Here is package.json:

 "firebase": "^5.8.0",
    "react": "16.6.3",
    "react-native": "0.57.8",
    "react-native-elements": "^0.19.1",
    "react-native-gesture-handler": "^1.0.15",
    "react-native-gifted-chat": "^0.7.0",
    "react-navigation": "^3.0.9"

There are a few online posts about the error with different situations. The error is pointing to GiftedChat in Chat.js. and I have no clue what causes the error.

import React, { Component} from 'react';
import {View, StyleSheet } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import Fire from '../Fire';

class Chat extends React.Component {
    static navigationOptions = ({ navigation}) => ({
        title: (navigation.state.params || {}).name || 'Chat!',
    });

    state = {
        messages: {},
    };

    componentDidMount() {
        Fire.shared.on(message =>
              this.setState(previousState => ({
                messages: GiftedChat.append(previousState.messages, message),
              }))
        );
    }

    componentWillUnmount() {
      Fire.shared.off();
    }

    get user() {
      // return our name and our UID for GiftedChat to parse
      return {
          name: this.props.navigation.state.params.name,
          _id: Fire.shared.uid,
      };
    }

    render() {
        return (
            <GiftedChat   <<<<==this is line:37
              messages={this.state.messages}
              onSend={Fire.shared.send}
              user={this.user}
            /> 
        );
    }
}

const styles = StyleSheet.create({});



export default Chat;
like image 798
user938363 Avatar asked Sep 16 '25 20:09

user938363


1 Answers

It looks like the error is in your initial state for messages.

You have defined your state as

state = { 
  messages: {} 
}

Messages should be defined as an array, so update your initial state to be

state = { 
  messages: []
}

From the documentation

  • messages (Array) - Messages to display

https://github.com/FaridSafi/react-native-gifted-chat#props

like image 104
Andrew Avatar answered Sep 19 '25 12:09

Andrew