简体   繁体   English

在 React Native 中获取“undefined is not and object”

[英]Getting “undefined is not and object” in React Native

So what I want is to have a list of TouchableOpacities which represent the days of the following week.所以我想要的是有一个 TouchableOpacities 列表,它代表下一周的几天。 The idea is, when I press any given day, its style changes between "selected" and "unselected"这个想法是,当我按下任何一天时,它的样式会在“选中”和“未选中”之间变化

Card with desired buttons for days带有几天所需按钮的卡片

My understanding is that (and I'm kind of a newbie), to force the component to re-render when I want the style to change, I must change the state.我的理解是(而且我是一个新手),当我想要改变样式时强制组件重新渲染,我必须改变 state。 And this state should be in the parent component, whose code is the following:而这个 state 应该在父组件中,其代码如下:

import React, { useState, useEffect } from "react";

import DaySelector from "./DaySelector";

const WeekList = () => {
  const [activeDays, setActiveDays] = useState([]);

  function onClick(index) {
    let days = [...activeDays];
    let day = days[index];
    days[index].active = !day.active;
    setActiveDays(days);
  }

  useEffect(() => {
    let date = new Date();
    let tempArr = [];

    for (let i = 0; i < 7; i++) {
      let temp = new Date();
      temp.setDate(date.getDate() + i);
      tempArr.push({
        key: temp.getDay(),
        day: temp,
        active: true,
        onPress: () => onClick(i),
      });
    }

    setActiveDays(tempArr);
  }, []);

  return (
    <>
      {activeDays.map((day) => (
        <DaySelector
          key={day.key}
          day={day.day}
          active={day.active}
          onClick={day.onPress}
        />
      ))}
    </>
  );
};

export default WeekList;

However, I keep getting the error in the title: "TypeError: undefined is not an object (evaluating 'day.active')", refering to line 10 of the 'onClick' (let day = days[index];) function, whose job is to toggle the "active" property the object, which should then propagate to the component, where a simple ternary operator uses it to choose the style to apply.但是,我不断收到标题中的错误:“TypeError: undefined is not an object (evalating 'day.active')”,指的是 'onClick' 的第 10 行(让 day = days[index];)function,其工作是切换 object 的“活动”属性,然后将其传播到组件,一个简单的三元运算符使用它来选择要应用的样式。

The thing is, I've console.logged the hell out of the program and it either works (but doesn't actually change the style of the DaySelector component) or it crashes, despite the logs saying "day" is indeed an object.问题是,我已经 console.logged 退出程序,它要么工作(但实际上并没有改变 DaySelector 组件的样式)要么崩溃,尽管日志说“day”确实是 object。

In short, I don't know what I'm doing wrong... and it seemed so simple at the beginning... Thanks in advance for any help!简而言之,我不知道自己做错了什么......一开始看起来很简单......在此先感谢您的帮助!

Setting the onPress function on the object state seemed fishy to me.在 object state 上设置 onPress function 对我来说似乎很可疑。 So I rewrote it without that and it works.所以我在没有它的情况下重写了它并且它有效。

  /**
   * Define the behavior of the component here.
   * Keep on data on the state.
   */
  const onClick = (index: number) => {
    // Update a field in an object in a list _without mutating_ the objects.
    setActiveDays([
      ...activeDays.slice(0, index),
      { ...activeDays[index], active: !activeDays[index].active },
      ...activeDays.slice(index + 1),
    ]);
  };

// Then

{activeDays.map((day: any, i: number) => (
 <DaySelector
  key={day.key}
  day={day.day}
  active={day.active}
  onClick={() => onClick(i)}
 />
))}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM