使用React Hook形式和Redux工具包的形式。然后路由React路由器DOM中的路由

发布于 2025-01-30 11:13:22 字数 1474 浏览 4 评论 0原文

我正在尝试使用React Hook表单和Redux工具包进行多步骤形式。 我的表格正常。他们正在发送数据。我的第一形式是在“/”路径上。这将完成,然后第二个形式在“/step2”路径上。但是,当第二形式完成后,路由将变为“/step2/step3”。为什么不是“/step3”? 3形式出现在“/step3”。我正在使用Usenavigate Hook。

       export const FormStep1 = () => {
          const dispatch = useDispatch();
          const navigate = useNavigate();
          const email = useSelector((state) => state.email);
          const password = useSelector((state) => state.password);
        
          const {
            handleSubmit,
            register,
            formState: { errors },
          } = useForm({
            resolver: yupResolver(schema),
            defaultValues: { email, password },
          });
        
          const submit = (data) => {
            dispatch(chooseEmail(data.email));
            dispatch(choosePassowrd(data.password));
            navigate("./step2");
          };
        

以下是我设置路线的方式

            function App() {
              return (
                <BrowserRouter>
                  <Routes>
                    <Route exact path="/" element={<FormStep1 />} />
                    <Route path="/step2" element={<FormStep2 />} />
                    <Route path="/step3" element={<FormStep3 />} />
                  </Routes>
                </BrowserRouter>
              );
            }
           
please guide where i am doing wrong.

I am trying to make a multi step form using react hook form and redux toolkit.
My forms are working fine. They are sending data. My 1st form is at "/" path. This gets completed then 2nd form comes at "/step2" path. But when 2nd form completes then routes becomes "/step2/step3". Why it is not "/step3" ? 3rd form comes at "/step3" . I am using useNavigate hook.

       export const FormStep1 = () => {
          const dispatch = useDispatch();
          const navigate = useNavigate();
          const email = useSelector((state) => state.email);
          const password = useSelector((state) => state.password);
        
          const {
            handleSubmit,
            register,
            formState: { errors },
          } = useForm({
            resolver: yupResolver(schema),
            defaultValues: { email, password },
          });
        
          const submit = (data) => {
            dispatch(chooseEmail(data.email));
            dispatch(choosePassowrd(data.password));
            navigate("./step2");
          };
        

Below is how i setup routes

            function App() {
              return (
                <BrowserRouter>
                  <Routes>
                    <Route exact path="/" element={<FormStep1 />} />
                    <Route path="/step2" element={<FormStep2 />} />
                    <Route path="/step3" element={<FormStep3 />} />
                  </Routes>
                </BrowserRouter>
              );
            }
           
please guide where i am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2025-02-06 11:13:22

从React-Router-dom文档中:

Either pass a To value (same type as <Link to>) with an **optional second { replace, state } arg or**

我相信问题是您在路由到 /step3时不会将第二个参数传递给Usenavigate()。

From the react-router-dom docs:

Either pass a To value (same type as <Link to>) with an **optional second { replace, state } arg or**

I believe the issue is that you are not passing the second argument to useNavigate() when routing to /step3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文