Возникли проблемы с переменной состояния в реагировать JS.Невозможно обновить значение переменной до true - PullRequest
0 голосов
/ 24 сентября 2018
import getAuthentication from './getAuthentication';

 class Home extends React. Component {
      constructor() {
        super();
        //this.authentication = false;
        this.state = {
          username: '',
          password: '',
          check:false,
          authentication:false
        };
        this.err = '';
      }



      componentDidUpdate() {
        console.log (this.state.authentication);
        console.log(this.state.authentication == true);

    if (this.state.check)
    {
        const promiseAuthentication = getAuthentication(
          this.state.username,
          this.state.password,
        );
        promiseAuthentication
          .then(response => {
            console.log (response.data.Success);
            console.log(response.data.Success == true);
            this.setState({check :false, authentication:response.data.Success});

          })
          .catch(error => {
            // console.log(error);
            this.err = error;
          });

      }

      if (this.state.authentication == true) {
        event.preventDefault();
        history.push('/overview');
       } 

      }

      assignUsername = event => {
        this.setState({ username: event.target.value });
      };

      assignPassword = event => {
        this.setState({ password: event.target.value });
      };

      handleSubmit = () => {
        this.setState({ check:true });
      };
==============================================================

getAuthentication.js

import axios from 'axios';

function getAuthentication(username, password) {
  const authenticationConfig = {
    Email: username,
    Password: password,
  };
  return axios.post(
    'http://localhost:5002/login/confirmation',
    authenticationConfig,
  );
}

export default getAuthentication;

В приведенном выше коде мой this.state.Authentication не обновляется до true

Я пытаюсь обновить его значение в axios обещании.

Может кто-нибудь подсказать, пожалуйстав чем дело?Я имею в виду, что я попробовал все, но я не могу продолжить.

Как изменить состояние объекта аутентификации и переключить новое окно?

У меня есть второй файл, который возвращает аксиосОбещание, где значение обещания "неопределено". Как выполнить асинхронный вызов и решить эту проблему ??

1 Ответ

0 голосов
/ 25 сентября 2018

componentDidUpdate заключено в if (this.state.check).Ничто в вставленном вами коде не устанавливает this.state.check в значение true.Набор this.state.check: true.

...