【发布时间】:2019-10-16 08:42:27
【问题描述】:
我正在使用带有 redux 的 react native 开发聊天应用程序,其中消息通过发送按钮发送。但是,每当我在点击发送按钮时发送消息时,TextInput 就不会被清除。
我想在点击发送按钮时清除TextInput 字段。在这里,我在 redux 中工作,所以我不想将 state 与 value 一起使用。
代码如下:
class Chat extends Component {
componentWillMount() {
this.props.fetchChat(this.props.receiverId);
}
message(text) {
this.props.writeMsg(text);
}
onSend = () => {
const { userId , receiverId, text } = this.props;
this.props.sendMessage({userId , receiverId, text});
}
render() {
return (
<View style={{ flex: 1 }}>
<FlatList
inverted={-1}
style={styles.list}
extraData={this.props}
data={this.props.convo}
keyExtractor = {(item) => {
return item.id;
}}
renderItem=
<ChatItem value={this.renderItem} />
/>
<MessageInput
onChangeText={text => this.message(text)}
onPress={this.onSend }
/>
</View>
);
}
}
这是组件MessageInput的代码:
<View style={inputContainer}>
<TextInput style={inputs}
placeholder="Write a message..."
onChangeText={onChangeText}
/>
</View>
<TouchableOpacity style={btnSend} onPress={onPress }>
<Icon
name='send'
type='MaterialIcons'
color='#fff'
style={iconSend}
/>
</TouchableOpacity>
【问题讨论】:
-
嗯,在 MessageInput 组件中不使用状态/值有什么好处?
标签: reactjs react-native redux react-native-textinput