【发布时间】:2021-11-06 01:29:39
【问题描述】:
我无法显示我通过 hendleSubmit 函数发布到 api 的消息。消息被发送并添加到数组中,但它不会立即显示,只有在我重新加载页面时才会显示。有人可以帮我解决这个问题吗?
这是我的代码:
export const ConversationForm = () => {
const { isLoading, data={} } = useQuery(['conversationForm'], () => conversationMessageService.getConversationMessages());
}
declaration of the component and getting data from api in 'data' array
<Grid container className={classes.conversation}>
<SadStates
states={[
{
when: isLoading,
render: <Loader/>
}
]}>
{data.items===undefined?
<img src={emptyChatIcon} alt={''}/>:
<ScrollToBottom mode='bottom' className={classes.scrollBottom}>
{data.items.map((message) => {
return (
<ConversationMessage
message={message}
key={message.id}
customerName={customerName}
userName={userName}/>
);
})}
</ScrollToBottom>
}
</SadStates>
</Grid>
调用子组件逐条打印消息
<Formik
initialValues={{
userPhone: userPhone,
customerPhone: customerPhone,
conversationMessageSender: conversationMessageSender,
body: '',
conversationMessageType: 1}}
onSubmit={handleSubmit}>
{({}) => (
<Form>
<Grid container>
<Grid item>
<ConversationInputField inputName='body'/>
</Grid>
<Grid item>
<button type='submit'>
<Grid item>
<img src={sendIcon} alt={''}/>
</Grid>
</button>
</Grid>
</Grid>
</Form>
)}
</Formik>
我在提交时调用 post 方法的表单
const handleSubmit = async( values, { setSubmitting }) => {
try {
await conversationMessageService.createConversatonMessage(values);
values.body='';
setSubmitting(false);
} catch (error) {
console.log(error);
}
};
处理提交函数
【问题讨论】:
标签: javascript reactjs rerender