我得到了一个基于react-native-gifted-chat的聊天,其中有一个儿童InputBox组件,该组件具有输入布局和一些按钮以及发送按钮。我传递了两个函数来处理onSend和摄像头,但我想知道如何将我在InputBox上编写的文本发送给包含GiftedChat的父代。
GiftedChat处理一组消息,但是如何根据输入和按钮onPress创建新的文本消息呢?
下面是我当前的代码:
在父级上
constructor(props) {
super(props)
this.handleCameraPress = this.handleCameraPress.bind(this);
this.onSend = this.onSend.bind(this);
this.state = {
chatData: {},
messages: []
}
}
onSend(messages = []) {
alert('sending message');
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messages),
}))
}
handleCameraPress() {
alert('camera tapped');
}
renderInputToolbar(props) {
return ( <InputBox
{...props}
messages={ this.messages }
onSend={ this.onSend }
handleCameraPress={ this.handleCameraPress }
containerStyle={ styles.inputToolbarStyle }
/>);
}这是GiftedChat的外观:
<GiftedChat
style={{ zIndex: 1 }}
messages={this.state.messages}
bottomOffset={Platform.OS === "ios" ? 335 : 0}
maxComposerHeight={150}
isAnimated={true}
user={{ _id: 1 }}
renderInputToolbar={ this.renderInputToolbar.bind(this) }
/>关于儿童
render() {
return (
<View style={ styles.container }>
<TouchableOpacity
activeOpacity={0.6}
style={styles.cameraButton}
onPress={ this.props.handleCameraPress }>
<Icon name='md-camera' style={ styles.cameraIcon } />
</TouchableOpacity>
<TextInput
style={ styles.textInput }
placeholder={i18n.t('chatInputPlaceholder')}
returnKeyType={'send'}
// onChangeText={ message => this.setState({ message })}
// value={this.props.message}
blurOnSubmit={false}
ref={'chatInputRef'}
/>
<Button
onPress={ this.props.onSend }
style={ styles.sendButton}>
<Icon name='md-send' style={ styles.sendIcon } />
</Button>
</View>
);
}我想我必须向this.props.onSend传递一个this.props.message?然后把它合并到父母的消息里?
发布于 2020-01-03 19:54:08
你必须创建一个状态变量,它将是你当前的消息,然后在Gifted chat中,你实现:
onInputTextChanged={text => {this.setState({typingMessage: text});}}因此,现在您的"this.state.typingMessage“将始终具有您在InputToolbar中写入的值
如果你有一个自定义的渲染器,你可以通过"props.text“访问如下的输入工具栏值:
renderSend(props) {
return (
<TouchableOpacity onPress={() => props.onSend({
_id: 10,
text: props.text,
createdAt: new Date(),
user: {
_id: 1,
name: 'Mike',
},
})}>
</TouchableOpacity>
);
}https://stackoverflow.com/questions/54330486
复制相似问题