React-native-gifted-chat: How to get value of textInput?

Created on 20 Dec 2016  路  8Comments  路  Source: FaridSafi/react-native-gifted-chat

How can I get the value of the text input. I'm using Firebase BTW.

<GiftedChat
      messages={this.state.messages}
      onSend={this.onSend}
      user={{
         _id:    this.props.user.id,
      }}
 />

onSend is called when sending a message.

onSend() {
            firestack.database.ref('chats').child(sha1(key[0] + key[1])).child('messages/' + res.key).set({
                _id: res.key,
                text: 'My message', 
                createdAt: new Date(),
                user: {
                    _id: this.props.user.id,
                    name: `${this.props.user.fname} ${this.props.user.sname}`,
                    avatar: this.props.user.pf,
                }
            })
    }

Which parameter can I use to get the textinout value?

Most helpful comment

When you select more than 1 picture, it also use the onSend passing an array of images selected. I think thats the reason why onSend receives an array of messages.
What I'm not sure is the reason on creating a message object on the GiftedChat onSend method and assigning a temporary id and some other data. I think that would be up to the parent to decide whats the better structure to give to the message, IMHO.

All 8 comments

@theobouwman currently the onSend method gives you an array with messages as first argument. So, you only need to use it.

Maybe something like this?

onSend(messages = []) {
  messages.forEach((message) => {
    firestack.database.ref('chats').child(sha1(key[0] + key[1])).child('messages/' + res.key).set({
      _id: res.key,
      text: message.text, 
      ...

@dgdavid why does it give an array instead of just the value?

To be honest, I don't know the reason for this, but is what I see in GiftedChat#L304. Although in Send#L22 the onPress action call this.props.onSend with an object that only contains the text.

Is possible that I'm wrong.

@kfiroo could clarify this better.

I wish I could :)
This is how it was originally implemented by @FaridSafi
I guess he had some plans for that, but I have no idea.

@dgdavid maybe we can create a PR to change that, to make the API clearer

Yes, we can. But before, we need to organize all pending things a little and assign tasks, are you agree?

Furthermore, this seems a breaking change, isn't that right? If I right, maybe is better to start a new branch for those kind of changes. What do you think?

and how to set a value to the input? I need to implement something like "Cite user" on bubble long press, but I don't know how to set the username in the text field

When you select more than 1 picture, it also use the onSend passing an array of images selected. I think thats the reason why onSend receives an array of messages.
What I'm not sure is the reason on creating a message object on the GiftedChat onSend method and assigning a temporary id and some other data. I think that would be up to the parent to decide whats the better structure to give to the message, IMHO.

You can find the textInput in messages[0].text in onSend(messages = []) method

Was this page helpful?
0 / 5 - 0 ratings