Получение следующей ошибки (TypeError: undefined не является объектом (Evaluating 'props.route.params.contactId)) - PullRequest
0 голосов
/ 19 июня 2020

Я получаю TypeError в следующем фрагменте кода: if (typeof props.route.params != "undefined") {

Я пытаюсь проверить наличие props.route.params, и если он существует, я пытаюсь получить и отфильтровать данные из redux.

In JS props.route.params не вызовет TypeError, но здесь он не работает, есть ли обходной путь?

  const EditContactScreen = props => {
  let contactId, editedContact;

  const [nameDropdown, setNameDropdown] = useState(false);
  const [name, setName] = useState();
  const [phone, setPhone] = useState();
  const [email, setEmail] = useState();
  const [address, setAddress] = useState();

  const { navigation } = props;

  if (typeof props.route.params != "undifined") {
    contactId = props.route.params.contactId;
    editedContact = useSelector(state => {
      for(let i = 0; i  < state.contacts.length; i++){
        if(contactId === state.contacts[i].id){
          return state.contacts[i];
        }
      }
    });
  }

Ответы [ 2 ]

0 голосов
/ 25 июня 2020

Проблема заключалась в том, что я неправильно написал «undefined» в следующей строке typeof props.route.params != "undifined". Поправил и заработало.

0 голосов
/ 19 июня 2020
  const EditContactScreen = props => {
  let contactId, editedContact;

  const [nameDropdown, setNameDropdown] = useState(false);
  const [name, setName] = useState();
  const [phone, setPhone] = useState();
  const [email, setEmail] = useState();
  const [address, setAddress] = useState();

  const { navigation } = props;

  if ('contactId' in props.route.params) {
    contactId = props.match.contactId;
    editedContact = useSelector(state => {
      for(let i = 0; i  < state.contacts.length; i++){
        if(contactId === state.contacts[i].id){
          return state.contacts[i];
        }
      }
    });
  }

Это должно работать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...