gpt4 book ai didi

javascript - React 最终形式 - 无限提交

转载 作者:行者123 更新时间:2023-12-05 07:16:46 25 4
gpt4 key购买 nike

如果我们尝试多次发送此表单,我们将无限次提交

如果我们在onSubmit 上设置sleep() 函数,一切正常。

为什么?怎样做才是对的?

import React from 'react'
import { render } from 'react-dom'
import { Form, Field } from 'react-final-form'

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

const onSubmit = async values => {
// Everything works fine with sleep()
// await sleep(100);
console.log('onSubmit...');
}

const App = () => (
<Form
onSubmit={onSubmit}
render={({ handleSubmit, submitting }) => (
<form onSubmit={handleSubmit}>
<Field name="notes" component="textarea" placeholder="Notes" />
<button type="submit" disabled={submitting}>
Submit
</button>
</form>
)}
/>
)

render(<App />, document.getElementById('root'))

最佳答案

要像正在做的那样同步处理提交,返回 undefined 即:

const onSubmit = values => {
console.log('onSubmit...');
return;
}

参见 https://final-form.org/docs/final-form/types/Config#onsubmit

关于javascript - React 最终形式 - 无限提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com