【问题标题】:Promise return from catch with Axios and final-form使用 Axios 和 final-form 从 catch 中返回 Promise
【发布时间】:2019-08-07 06:46:06
【问题描述】:

我正在使用 Axios 从 React 客户端登录 api 服务。姓名和密码表格由final-form 处理。一切都按预期工作,除非我想从 onSubmit 函数返回错误。

有两个组件:一个父组件Login,它使用logIn 函数处理对API 的调用,以及一个嵌套组件LoginUi,它具有表单和一个onSubmit 函数,它调用父方法logIn 通过this.props.logIn():

这里是父Login组件中的方法logIn:

class Login extends Component {
    constructor() {
        super();
        this.logIn = this.logIn.bind(this);
    }

    logIn(credentials) {
        return axios({
            method: 'post',
            url: 'http://0.0.0.0:3000/v1/login/',
            data: {
                name: credentials.name,
                password: credentials.password,
            },
        })
            .then((response) => {
                return response;
            })
            .catch((error) => {
                return error;
            });
    }

    render() {
        return <LoginUi logIn={this.logIn} {...this.props} />;
    }
}

export default Login;

这里是子LoginUi组件中的方法onSubmit:

class LoginUi extends Component {
    constructor(props) {
        super(props);
        this.onSubmit = this.onSubmit.bind(this);
    }

    onSubmit(credentials) {
        this.props
            .logIn(credentials)
            .then((result) => {
                console.log(result);
            })
            .catch((error) => {
                console.log(error);

                return { [FORM_ERROR]: 'Login Failed' };
            });
    }

    render() {
        return (
            <div className="LoginUi">
                {/* here goes the form with final-form, not included for brevity */}
            </div>
        );
    }
}

export default LoginUi;

{ [FORM_ERROR]: 'Login Failed' } 负责更改表单的状态——由final-form 处理——但它没有这样做。如果我在catch 之外返回它,它可以工作:

onSubmit(credentials) {
    this.props
        .logIn(credentials)
        .then((result) => {
            console.log(result);
        })
        .catch((error) => {
            console.log(error);
        });
    return { [FORM_ERROR]: 'Login Failed' };
}

但显然这不是我想要的,因为只有在 API 调用返回错误时才必须返回 [FORM_ERROR]: 'Login Failed'。

我很确定这里使用 Promise 存在问题。如果有人有任何想法,我将不胜感激!

谢谢!

【问题讨论】:

  • 返回的用例是什么?你为什么不setState 赶上?
  • 你如何使用/调用onSubmit?

标签: javascript reactjs promise es6-promise react-final-form


【解决方案1】:

由于您依赖 Promises,因此 onSubmit 应该返回一个 Promise。将return 添加到 onSubmit 否则它返回 undefined 并且final-form 无法知道 axios 调用已完成:

onSubmit(credentials) {
  return this.props
    .logIn(credentials)
    .then((result) => {
      console.log(result);
    })
    .catch((error) => {
      console.log(error);    
      return { [FORM_ERROR]: 'Login Failed' };
    });
}

【讨论】:

  • 一个由on 开头的事件处理程序不应该返回任何东西。 imo OP 应该更新传递给then 和catch 的回调中的状态。
  • @Thomas 基于doc onSubmit 可以由final-form 直接通过createForm 调用使用。在这种情况下,不需要显式 setState 调用。我认为这就是使用final-form 的全部意义所在。
  • 实际上,Final Form 提供了一种通过回调而不是承诺返回错误的方法,如果您愿意的话。 onSubmit 函数的第三个参数是回调函数。 github.com/final-form/…
猜你喜欢
  • 2016-12-27
  • 1970-01-01
  • 2018-02-21
  • 2017-09-13
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2021-07-23
相关资源
最近更新 更多