redux 如何在react router v6中使用“withRouter”和“history push”进行重定向

osh3o9ms  于 2022-12-13  发布在  React
关注(0)|答案(1)|浏览(117)

我注册了一个按钮,单击该按钮后,用户将进入登录页面。我尝试使用history.push()并包含输入到表单中的值,但我不知道如何传递userData的值,因为我得到了以下错误:
refers this line payload: err.response.data()
当我将此行更改为在控制台中显示数据时,它显示正确。

  • 身份验证操作.js*
import axios from 'axios';

import { GET_ERRORS} from "./types";
import {useLocation, useNavigate, useParams} from "react-router-dom";
import React from "react";

 // Register User
export const registerUser = (userData,history) => dispatch => {
  axios
    .post('/api/users/register', userData)
      .then(res => history.push('/login'))
      // .then(res => console.log(res.data))
    .catch(err =>
      dispatch({
          type: GET_ERRORS,
          payload: err.response.data()
      })
    );
};`

我试图导入路由器的钩子版本,但它带来了另一个错误“无效钩子调用警告”

export const registerUser = (userData,history) => dispatch => {
    const navigate = useNavigate();
  axios
    .post('/api/users/register', userData,navigate)

      .then(res =>navigate('/login'))
      // .then(res => console.log(res.data))
    .catch(err =>
      dispatch({
          type: GET_ERRORS,
          payload: err.response.data()
      })
    );
};

我做错了什么?

ffx8fchx

ffx8fchx1#

错误请求错误意味着API可能需要一个您没有发送的字段,或者字段的数据格式错误。

相关问题