我別無選擇,只能在處理程序中呼叫獲取器的提交方法。它正在存取路由器中正確的操作方法,但是我無法將該方法傳遞給操作,即表單中定義的 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> );