首页  >  问答  >  正文

检索 Router 6 useFetcher 表单中的方法

我别无选择,只能在处理程序中调用获取器的提交方法。它正在访问路由器中正确的操作方法,但是我无法将该方法传递给操作,即表单中定义的 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粉993712159P粉993712159381 天前444

全部回复(1)我来回复

  • P粉739942405

    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>
    );
    

    回复
    0
  • 取消回复