reactjs - 如何只发送图片消息? (也无需发送文本)

标签 reactjs react-native react-native-image-picker react-native-gifted-chat

我只想发送图片而不输入任何文字? 我正在使用 react-native-image-picker,我目前可以发送带有文本的图像,但是如果我只想发送图像怎么办? 我试过 alwaysShowSend 并尝试发送但没有任何反应,除非文本字段不为空 文档对此非常模糊......

      <GiftedChat
        messages={friendMsgs}
        onSend={messages => this.onSend(messages, this.state.image)}
        user={{
          _id: user && user.id,
          name: user && user.firstName,
          avatar: user && user.profilemage
        }}
        text={this.state.text}
        alwaysShowSend={
          this.state.text ? true : false || this.state.image ? true : false
        }
        onInputTextChanged={text => this.setState({ text })}
        renderLoading={() => <Loading />}
        onLoadEarlier={this.loadMessages.bind(this, userParams)}
        isLoadingEarlier={loading}
        isAnimated
        renderAvatarOnTop
        loadEarlier={friendMsgs.length >= 20}
        scrollToBottom
        scrollToBottomComponent={() => (
          <Ionic name='ios-arrow-round-down' size={30} color='#000' />
        )}
        extraData={this.state}
        renderBubble={props => {
          const color = props.currentMessage.read ? '#0084ff' : '#389bff';
          return (
            <Bubble
              {...props}
              wrapperStyle={{ right: { backgroundColor: color } }}
            />
          );
        }}
        renderActions={() => (
          <Feather
            style={styles.uploadImage}
            onPress={this.uploadImage}
            name='image'
            size={30}
            color='#000'
          />
        )}
      />


  onSend(messages = [], image) {
    const { socket, user, navigation } = this.props;
    const { friendMsgs } = this.props.history;

    const receiver = navigation.getParam('user');

    if (socket && socket.readyState === 1) {
      const msg = {
        ...messages[0],
        image
      };

      const sendMsg = GiftedChat.append(friendMsgs, msg);

      const data = {
        senderId: user && user.id,
        receiverType: 'user',
        messageType: 'text',
        receiverId: receiver.id,
        read: false,
        content: messages[0].text
      };

      this.props.sendMsg(data, sendMsg);
    }
  }

最佳答案

你能试试这个吗?

 const msg = {
     createdAt: new Date(),
     user: user && user.id,
     image: image,
     sent: true,
     received: true,
   }
...
const sendMsg = GiftedChat.append(friendMsgs, msg);
const data = {
        senderId: user && user.id,
        receiverType: 'user',
        receiverId: receiver.id,
        read: false,
      };

      this.props.sendMsg(data, sendMsg);
this.props.sendMsg({});

关于reactjs - 如何只发送图片消息? (也无需发送文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57611845/

相关文章:

android - 如何在React-native中将图像从图库复制到应用程序的安装目录

android - iOS 中的模块 react-native-image-picker 总是需要麦克风权限,尽管我没有选择视频,只有摄像头

javascript - 在reactjs中没有父子关系的不同组件之间进行通信

javascript - ReactJS - 外部 JS 函数 "is not a function"

javascript - 如何在React中的功能组件中制作与componentDidUpdate函数类似的函数?

javascript - React Native WebP 格式图像有时会消失

javascript - Firebase 存储 : string does not match format base64: invalid character found. 仅当调试关闭时

javascript - 使用 Redux-Form 进行搜索过滤器,我如何才能 "reset"并重新提交表单?

javascript - 如何在获取 promise 中的错误时捕获网络错误

react-native - 如何在 React Native Android 的发布包中包含静态 html?