如何取消取消所有订阅和异步任务?

How to cancel cancel all subscriptions and asynchronous tasks?

在登录页面输入凭据后,我被定向到预期页面,但控制台中显示此错误。如果我理解正确的话,这是由 Formik 和组件未卸载引起的?我搜索了有类似错误的用户,但我似乎可以找到错误的修复方法。

Console Error

import React, { useState, useEffect } from 'react';
import { Form, Field, withFormik } from 'formik';
import Axios from 'axios';
import * as Yup from 'yup';

import './signin.scss';

const SignIn = ({ errors, touched, values, status }) => {
  const [user, setUser] = useState([]);
  // console.log(user);

  useEffect(() => {
    if (status) {
      setUser([...user, status]);
    }
  }, [user, status]);

  return (
    <div className='loginForm'>
      <h1>Log In</h1>
      <Form>
        <label className='inputLabels' htmlFor='username'>
          <b>Username/Email:</b>
          <Field
            className='inputFields'
            component='input'
            type='text'
            name='username'
            placeholder='Chef213'
          />
          {touched.username && errors.username && <p>{errors.username}</p>}
        </label>
        <br />
        <label className='inputLabels' htmlFor='password'>
          <b>Password:</b>
          <Field
            className='inputFields'
            component='input'
            type='password'
            name='password'
            placeholder='password1'
          />
          {touched.password && errors.password && <p>{errors.password}</p>}
        </label>
        <br />
        <div className='buttons'>
          <button type='submit'>Log In</button>
        </div>
      </Form>

      {user.map(users => (
        <p key={users.id}>{users.FirstName} You have successfully logged in.</p>
      ))}
    </div>
  );
};

const FormikSignIn = withFormik({
  mapPropsToValues({ username, password }) {
    return {
      username: username || '',
      password: password || ''
    };
  },

  validationSchema: Yup.object().shape({
    username: Yup.string().required('Username is required'),
    password: Yup.string()
      .required('Password is required')
      .min(8, 'Password is too short - should be 8 chars minimum.')
      .matches(/[a-zA-Z]/, 'Password can only contain Latin letters.')
  }),

  handleSubmit(values, { props, setStatus }) {
    console.log('Form submited', values);
    Axios.post('https://bw-how-to.herokuapp.com/login', values)
      .then(res => {
        // console.log(res);
        if (res.status === 200) {
          props.history.push(`/homepage`);
          setStatus(res.data);
        }

        // resetForm();
      })
      .catch(err => console.error('Error', err.response));
  }
})(SignIn);

export default FormikSignIn;

更正此错误的最佳方法是什么?

我假设您使用的是 React Router?我认为您可以通过翻转 setStatusprops.history.push 来解决您的问题。发生的情况是您正在导航到一个新页面,然后尝试设置状态。如果先设置状态,然后导航,应该可以解决该错误消息。

handleSubmit(values, { props, setStatus }) {
    console.log('Form submited', values);
    Axios.post('https://bw-how-to.herokuapp.com/login', values)
      .then(res => {
        // console.log(res);
        if (res.status === 200) {
          setStatus(res.data);
          props.history.push(`/homepage`);
        }

        // resetForm();
      })
      .catch(err => console.error('Error', err.response));