我别无选择,只能在处理程序中调用获取器的提交方法。它正在访问路由器中正确的操作方法,但是我无法将该方法传递给操作,即表单中定义的 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='/'> ..............
P粉7399424052023-09-07 13:20:35
尝试这个解决方案,将方法传递给 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> );