【问题标题】:How do you autofocus the input box in react-native-gifted-chat你如何在 react-native-gifted-chat 中自动对焦输入框
【发布时间】:2018-10-22 05:37:56
【问题描述】:

我正在使用有天赋的聊天库,并希望在初始渲染时将键盘自动聚焦在输入上。我看到有一个命令式函数focusTextInput,但是我该如何调用它呢?

<GiftedChat
  {...props}
  messages={this.state.messages}
  ref={(chat) => this.chat = chat }
  onSend={messages => this.onSend(messages)}
  user={{
      _id: 1,
  }}
/>

Github Repo

我尝试设置 ref 并直接在安装时调用它,但没有成功。

【问题讨论】:

    标签: react-native react-native-gifted-chat


    【解决方案1】:

    因为图书馆已经有textInputProps

    textInputProps (Object) - 要传递给TextInput的额外道具

    因此您可以使用TextInputautoFocus 属性

    <GiftedChat
          textInputProps={{autoFocus: true}}
          messages={this.state.messages}
          onSend={messages => this.onSend(messages)}
          user={{
                 _id: 1,
           }}
      />
    

    【讨论】: