React Native:null不是一个对象(评估'_this.state.displayErrors')

时间:2019-09-05 20:22:46

标签: javascript react-native

我有一个功能齐全的Android应用程序,现在出现此错误:

null is not an object (evaluating '_this.state.displayErrors')在这里引用此行代码:

_getErrors = () => {
    if (this.state.displayErrors) {
      return {
        ...this.state.validationErrors,
        ...this.props.validationErrors
      };
    }
    return {};
  };

这是完整文件:

import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import DetailsConfirmationForm from "auth/components/DetailsConfirmationForm";
import {
  firstNameChanged,
  lastNameChanged,
  prefixChanged,
  suffixChanged,
  stateChanged
} from "auth/registrationActions";
import regex from "utils/helpers/regex";
import { prefixes, suffixes } from "enums/dropdownOptions";

export class DetailsConfirmation extends Component {
  static propTypes = {
    firstName: PropTypes.string,
    firstNameChanged: PropTypes.func.isRequired,
    lastName: PropTypes.string,
    lastNameChanged: PropTypes.func.isRequired,
    navigation: PropTypes.object,
    prefix: PropTypes.string,
    prefixChanged: PropTypes.func.isRequired,
    registeredUser: PropTypes.object,
    state: PropTypes.string,
    stateChanged: PropTypes.func.isRequired,
    suffix: PropTypes.string,
    suffixChanged: PropTypes.func.isRequired,
    validationErrors: PropTypes.object
  };

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { personalDetails, personalAddress } = this.props.registeredUser;
    console.log(this.props.registeredUser);
    if (personalDetails) {
      this.props.firstNameChanged(personalDetails.firstName);
      this.props.lastNameChanged(personalDetails.lastName);
      this.props.suffixChanged(personalDetails.suffix);
      this.props.prefixChanged(personalDetails.prefix);
    }

    if (personalAddress && personalAddress.stateCode) {
      this.props.stateChanged(personalAddress.stateCode);
    }

    const { params = {} } = this.props.navigation.state;
    const { displayAlert = true } = params;

    this.state = {
      validationErrors: {},
      displayErrors: false,
      titleName: personalDetails && personalDetails.firstName,
      displayAlert
    };
  }

  componentWillReceiveProps(nextProps) {
    if (this.state.displayErrors) {
      this._validate(nextProps);
    }
  }

  _validate = props => {
    const { prefix, state, firstName, lastName, suffix } = props;
    const validPrefixes = prefixes.map(p => p.value);
    const validSuffixes = suffixes.map(p => p.value);
    const validationErrors = {
      prefix:
        prefix && prefix.trim() && validPrefixes.includes(prefix)
          ? ""
          : "Is Required",
      state: state && state.trim() ? "" : "Is Required",
      firstName: firstName && firstName.trim() ? "" : "Is Required",
      lastName: lastName && lastName.trim() ? "" : "Is Required",
      suffix:
        !suffix || validSuffixes.includes(suffix) ? "" : "Select an option"
    };

    const nameRegexErrorMessage =
      "Only letters, hyphens and periods are allowed.";
    if (validationErrors.firstName === "" && !regex.userName.test(firstName)) {
      validationErrors.firstName = nameRegexErrorMessage;
    }
    if (validationErrors.lastName === "" && !regex.userName.test(lastName)) {
      validationErrors.lastName = nameRegexErrorMessage;
    }

    const fullErrors = {
      ...validationErrors,
      ...this.props.validationErrors
    };

    const isValid = Object.keys(fullErrors).reduce((acc, curr) => {
      if (fullErrors[curr] !== "") {
        return false;
      }

      return acc;
    }, true);

    if (isValid) {
      this.setState({ validationErrors: {} });
      //register
    } else {
      this.setState({ validationErrors, displayErrors: true });
    }

    return isValid;
  };

  _navigate = () => {
    const isValid = this._validate(this.props);
    if (isValid) {
      if (this.props.registeredUser.organization) {
        this.props.navigation.navigate("CompleteAccount");
      } else {
        this.props.navigation.navigate("AskForMembership");
      }
    }
  };

  _getErrors = () => {
    if (this.state.displayErrors) {
      return {
        ...this.state.validationErrors,
        ...this.props.validationErrors
      };
    }
    return {};
  };

  render() {
    return (
      <DetailsConfirmationForm
        {...this.state}
        {...this.props}
        navigate={this._navigate}
        validationErrors={this._getErrors()}
      />
    );
  }
}

const mapsStateToProps = ({ registrations }) => {
  return {
    ...registrations.accountData,
    validationErrors: registrations.validationErrors,
    registeredUser: registrations.registeredUser
  };
};

export default connect(
  mapsStateToProps,
  {
    firstNameChanged,
    lastNameChanged,
    prefixChanged,
    suffixChanged,
    stateChanged
  }
)(DetailsConfirmation);

这是范围问题吗? if语句不能访问displayErrors函数之外的_getErrors吗?如果是这样,那么在过去的几个星期中,这项工作是如何进行的?

我尝试放置:

this.state = {
   validationErrors: {},
   displayErrors: false,
   titleName: personalDetails && personalDetails.firstName,
   displayAlert
};
我认为它属于constructor(props)函数中的

,但是随后我遇到了很多有关其中变量的问题,例如personalDetailsdisplayAlert没有被定义为变量。最大的痛苦是displayAlert

2 个答案:

答案 0 :(得分:1)

要在构造函数中设置this.state,您将需要解构props以获取所需的值,类似于componentDidMount中发生的情况。我将从componentDidMount中完全删除初始状态值的设置。

constructor(props) {
    super(props);
    const { personalDetails, personalAddress } = props.registeredUser;
    const { params = {} } = props.navigation.state;
    const { displayAlert = true } = params;

    this.state = {
      validationErrors: {},
      displayErrors: false,
      titleName: personalDetails && personalDetails.firstName,
      displayAlert
    };
  }

  componentDidMount() {
    const { personalDetails, personalAddress } = this.props.registeredUser;
    console.log(this.props.registeredUser);
    if (personalDetails) {
      this.props.firstNameChanged(personalDetails.firstName);
      this.props.lastNameChanged(personalDetails.lastName);
      this.props.suffixChanged(personalDetails.suffix);
      this.props.prefixChanged(personalDetails.prefix);
    }

    if (personalAddress && personalAddress.stateCode) {
      this.props.stateChanged(personalAddress.stateCode);
    }
  }

我不确定这是否可以解决您的问题,或者不确定如何将this.state设置为null

答案 1 :(得分:0)

我认为问题在于 DetailsConfirmationForm 组件中的这一行

validationErrors={this._getErrors()}

您正在调用函数而不是传递

尝试

validationErrors={this._getErrors}