android - 在 this.state.message 中显示接收者消息

标签 android reactjs react-native state

我正在使用 react-native-gifted-chat( https://github.com/FaridSafi/react-native-gifted-chat ) 在我的应用程序上创建一个聊天界面,我想从我的数据库加载消息,但是我如何在 react-native-gifted-chat 中指定它接收者发送的消息不是发送者,即在应用加载之前,消息被加载到 this.state.messages,根据文档,这应该是这样的

 {
 _id: 1,
 text: 'My message',
 createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
 user: {
 _id: 2,
 name: 'React Native',
 avatar: 'https://facebook.github.io/react/img/logo_og.png',
 },
 image: 'https://facebook.github.io/react/img/logo_og.png',
 // Any additional custom parameters are passed through
 }

文档只说明了如何指定发送者的消息,没有说明接收者的消息,请问如何显示接收者的消息,即

  {
  _id: 2,
  text: 'Receiver's message',
  createdAt: new Date(Date.UTC(2016, 6, 11, 17, 20, 0)),
  user: {
   _id: 2,
  name: 'Receiver',
  avatar: 'https://facebook.github.io/react/img/logo_o3.png',
  },
  image: 'https://facebook.github.io/react/img/logo_o3.png',
  }

最佳答案

https://github.com/FaridSafi/react-native-gifted-chat/blob/master/example/App.js中有一个例子你可以在那里看到一个 onReceive() 函数。

 onReceive(text) {
    this.setState((previousState) => {
      return {
        messages: GiftedChat.append(previousState.messages, {
          _id: Math.round(Math.random() * 1000000),
          text: text,
          createdAt: new Date(),
          user: {
            _id: 2,
            name: 'React Native',
            // avatar: 'https://facebook.github.io/react/img/logo_og.png',
          },
        }),
      };
    });
  }

你可以在收到消息时像这样触发这个函数。(当状态改变时)

关于android - 在 this.state.message 中显示接收者消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53028015/

相关文章:

android - 在 Android 中使用 MediaRecorder 录制的音频文件无法在 PC 上正常播放

javascript - 类型错误 : Cannot read property state of null

Android java.lang.UnsatisfiedLinkError : couldn't find DSO to load

rest - Yii2 REST api 承载认证

javascript - if else 条件使用类名

react-native - 无法弹出博览会以对 native 使用react

android - React Native 键盘白色闪烁

android - Xamarin.forms(或)xamarin.ios/xamarin.android(或) native

Android:带有 RecyclerView 方向更改错误的 fragment

java - 无法从 Firebase 存储下载图像