javascript - 如何使用 renderMessage、renderMessageImage 函数以及如何在消息对象为空时显示未找到的消息

标签 javascript react-native react-native-gifted-chat

我正在将react-native-gifted-chat集成到我的应用程序中。

我的天才聊天代码是

     <GiftedChat
                composerHeight={COMPOSER_HEIGHT}
                minInputToolbarHeight={COMPOSER_HEIGHT}
                messages={this.state.messages}
                onSend={messages => this.onSend(messages)}
                user={{ _id: this.state.senderUserName }}
                loadEarlier={this.state.loadEarlier}
                isLoadingEarlier={this.state.isLoadingEarlier}
                onLoadEarlier={this.onLoadEarlier}
                placeholder="Type your message"
                renderSend={this.renderSend}
                alwaysShowSend={true}
                renderActions={this.imageSend.bind(this)}
                renderInputToolbar={this.renderInputToolbar}
                renderBubble={this.renderBubble.bind(this)}
                renderMessage={this.renderMessage.bind(this)}
                renderMessageImage={this.renderMessageImage}
                renderAvatar={null}
                inverted={true}
            /> 

这里我需要使用自定义图像渲染器。我知道我需要使用 renderMessageImage 但我无法找到正确的示例来实现它。

My RenderMessageImage is 

        renderMessage(props) {
           if(this.state.messages.length !==0){
             return <Message {...props} 
             />;
             }else{
               return <View style={{flex:1, backgroundColor:'red'}}>
                 <Text>Hello no data found</Text>
               </View>
                  }
                return null
                    }

但它不起作用。

我的另一个问题是,如果没有任何消息,我需要显示有天赋的聊天屏幕,因为找不到消息而不是白屏。我怎样才能实现这两个。

我需要类似的屏幕

enter image description here

最佳答案

您可以尝试从样式中删除 flex:1 可能是一个解决方案。

编辑

使用系统消息,如果messages.length === 0,则将其包含在消息列表中。

编辑2

您必须创建一个叠加层,请参阅我的示例:https://snack.expo.io/@xcarpentier/gifted-chat

render() {
    return (
      <>
      {this.state.messages.length === 0 && (
        <View style={[
          StyleSheet.absoluteFill,
          {
            backgroundColor: 'white',
            justifyContent: 'center',
            alignItems: 'center',
            bottom: 50
          }]}>
          <Image 
            source={{ uri: '/image/qLdPt.png' }}
            style={{
              ...StyleSheet.absoluteFillObject,
              resizeMode: 'contain'
            }}
          />
      </View>
      )}
      <GiftedChat
       messages={this.state.messages}
       onSend={(messages) => this.onSend(messages)}
       renderCustomView={this.renderCustomView}
       user={{
         _id: 1,
       }}
       parsePatterns={linkStyle => [
          {
            pattern: /#(\w+)/,
            style: { ...linkStyle, color: 'lightgreen' },
            onPress: props => alert(`press on ${props}`),
          },
        ]}
     />
     </>
    );
  }

关于javascript - 如何使用 renderMessage、renderMessageImage 函数以及如何在消息对象为空时显示未找到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529508/

相关文章:

javascript - 如何在气泡聊天 react native 有天赋的聊天下渲染项目时间?

javascript - Angular 过滤阵列

javascript - cmi.core.lesson_location 没有返回我存储在其中的相同字符串?

javascript - 在 React Native 中将文本置于图像下方

css - react native : trying to hide search bar on scroll

javascript - 带有云 Firestore 分页的 React-native-gifted-chat

javascript - 在 ajax 请求中,url 中的前面的 `/` 指定了什么?

javascript - React-Native-snap-carousel 检查条件与状态值?

javascript - 无法从 componentDidMount 调用函数