我试图获取状态,然后通过API调用将其推送到服务器,但是如果未定义状态,则无法执行此操作。
我正在执行以下操作:
show interface
在<GiftedChat
messages={this.state.messages}
renderUsernameOnMessage={true}
text={this.state.message}
onInputTextChanged={message => this.setState({ message })}
placeholder='Type your message here...'
renderBubble={props => {
return (
<Bubble
{...props}
textStyle={{
right: {
color: 'white',
},
}}
wrapperStyle={{
left: {
backgroundColor: 'white',
},
right: {
backgroundColor: 'black'
}
}}
/>
);
}}
user={{
_id: this.state.userid,
}}
scrollToBottom
scrollToBottomComponent={() => (
<Ionicons name='ios-arrow-round-down' size={30} color='#000' />
)}
onSend={async () => {
const token = await AsyncStorage.getItem('access');
const access = 'Bearer ' + token;
axios.post(`http://site.test/api/auth/send/group/${this.props.navigation.getParam('id')}`,
{
message: this.state.message,
groupId: this.props.navigation.getParam('id'),
},
{
headers: {
'Authorization': access,
}
}).catch(err => {
console.log(err);
})
}}
/>
上,我正在执行以下操作:
onSend
我在这里做错什么了吗?
答案 0 :(得分:0)
让我们尝试一下,将resetState用于传递给的消息,以更新ui,然后将其发送到api调用
import { GiftedChat } from 'react-native-gifted-chat';
onSend={(message) => {
this.setState({
message: GiftedChat.append(this.state.message, message)
}, async () => {
const token = await AsyncStorage.getItem('access');
const access = 'Bearer ' + token;
axios.post(`http://site.test/api/auth/send/group/${this.props.navigation.getParam('id')}`,
{
message: this.state.message,
groupId: this.props.navigation.getParam('id'),
},
{
headers: {
'Authorization': access,
}
}).catch(err => {
console.log(err);
})
})
}}