setPost
改变
post
钩子中的值,该值由组件消耗。这会触发重新渲染,从而导致无限循环。
要解决这个问题,你需要打电话
路标
只有有限的次数。你可以通过使用
useEffect
只能在启动时调用。
function Post({ activePostId }) {
const { status, post, setPost, error, refetch } = usePost(activePostId)
const [savePost, savePostStatus] = useSavePost()
useEffect(() => {
setPost('SetPost')
}, []); // <- the empty array here ensures that the setPost is called once, if you want it called on other dependency changes, you can add them here.
const onSubmit = async (values) => {
try {
await savePost(values)
refetch()
} catch (err) {
console.error(err)
}
}