I have no other option but to call the submit method for a fetcher in a handler. It’s accessing the correct action method in the routers however I’m unable to pass the method to the action, i.e. method='POST
defined in the Form. How to access the method of fetch.Form
inside the handler?
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='/'>
..............
2
Answers
Try this solution, you pass the method to the
handlerLogin
function:handlerLogin
is being memoized and is closing over thefetcher
value from the initial render cycle.fetcher
is an external dependency so it should be included in theuseCallback
hook’s dependency array.Since this is likely only passed to the RRD
Form
component and not down as a prop to children I suspect theuseCallback
hook is even necessary and could likely be removed.