使用React Hook形式和Redux工具包的形式。然后路由React路由器DOM中的路由
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从React-Router-dom文档中:
我相信问题是您在路由到 /step3时不会将第二个参数传递给Usenavigate()。
From the react-router-dom docs:
I believe the issue is that you are not passing the second argument to useNavigate() when routing to /step3.