我别无选择,只能在处理程序中调用获取器的提交方法。它正在访问路由器中正确的操作方法,但是我无法将该方法传递给操作,即表单中定义的 method='POST 。如何访问处理程序内的fetch.Form方法?
const fetcher = useFetcher()
const handlerLogin = useCallback(async () => {
console.log(fetcher.formMethod) //-> outputting undefined
fetcher.submit({ value: 'social' }, { method: fetcher.formMethod })
},[])
return (
<Card className={styles.loginCard}>
<fetcher.Form method='POST' action='/'>
.............. Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
尝试这个解决方案,将方法传递给
handlerLogin函数:const fetcher = useFetcher(); const handlerLogin = useCallback(async (formMethod) => { fetcher.submit({ value: 'social' }, { method: formMethod }); }, []); return ( <Card className={styles.loginCard}> <fetcher.Form method='POST' action='/' > {/* other parts ... */} <button onClick={() => handlerLogin(fetcher.formMethod)}> Submit </button> </fetcher.Form> </Card> );