@lexstile

Как исправить предупреждение: Can't perform a React state update on an unmounted component?

При сабмите формы вылезает предупреждение:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

Есть компонент Login
export const LoginPage = () => {
  const dispatch = useDispatch();
  const history = useHistory();

  const onSubmit = async (fields, actions) => {
    await dispatch(loginUser(fields));
    actions.setSubmitting(false);
  };

  return (
    <View id="login" activePanel="main">
      <Panel id="main">
        <PanelHeader>Авторизация</PanelHeader>
        <Group>
          <Formik
            validationSchema={LoginSchema}
            initialValues={{ [FIELDS.EMAIL]: '', [FIELDS.PASSWORD]: '' }}
            onSubmit={onSubmit}
          >
            {({ dirty, isValid, isSubmitting }) => (
              <Form>
                <Field name={FIELDS.EMAIL}>
                  {({ field, meta }) => (
                    <FormItem top="Email" status={getFieldStatus(meta)} bottom={meta.touched && meta.error}>
                      <Input type="text" placeholder="Укажите Ваш email" {...field} />
                    </FormItem>
                  )}
                </Field>
                <Field name={FIELDS.PASSWORD}>
                  {({ field, meta }) => (
                    <FormItem top="Пароль" status={getFieldStatus(meta)} bottom={meta.touched && meta.error}>
                      <Input type="password" placeholder="Введите пароль" {...field} />
                    </FormItem>
                  )}
                </Field>
                <FormItem>
                  <Button
                    stretched
                    type="submit"
                    size="l"
                    before={isSubmitting && <Spinner size="small" style={{ color: 'var(--white)' }} />}
                    disabled={!dirty || !isValid || isSubmitting}
                  >
                    Войти
                  </Button>
                </FormItem>
                <FormItem style={{ textAlign: 'center' }}>
                  <Link onClick={() => history.push(GUEST_ROUTES.RESTORE)}>Забыли пароль?</Link>
                </FormItem>
                <FormItem style={{ textAlign: 'center' }}>
                  <Link onClick={() => history.push(GUEST_ROUTES.REGISTER)}>Создать новый аккаунт</Link>
                </FormItem>
              </Form>
            )}
          </Formik>
        </Group>
      </Panel>
    </View>
  );
};

loginUser
export const loginUser = (data) => async (dispatch) => {
  const [execute] = apiShell(callLogin, dispatch);
  const response = await execute(data);

  const accessToken = get(response, 'access_token');

  if (accessToken) {
    saveToken(accessToken);
    dispatch(userSlice.actions.setRole(getRole(accessToken)));
  }
};

userSlice
const initialState = {
  role: getRole(),
};

export const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    setRole: (state, action) => {
      state.role = action.payload;
    },
  },
});

62cea3cdf00e5707903716.png
  • Вопрос задан
  • 91 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы