我別無(wú)選擇,只能在處理程序中呼叫獲取器的提交方法。它正在存取路由器中正確的操作方法,但是我無(wú)法將該方法傳遞給操作,即表單中定義的 method='POST
。如何存取處理程序內(nèi)的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='/'> ..............
嘗試這個(gè)解決方案,將方法傳遞給 handlerLogin
函數(shù):
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> );