TypeError: undefined is not an object (evaluating '_ref.painLevelColor')

TypeError: undefined is not an object (evaluating '_ref.painLevelColor')

我在 React Native 中有一个功能组件,它正在接收其 parent 状态作为道具。

const PainLevel = ({painLevelColor = 'green', setPainLevelColor}) => {...}

parent:

const BodyParts = () => {
  const [side, setSide] = useState('Front');
  const [painLevelColor, setPainLevelColor] = useState('#43BA08');
return(
      {...}
      <PainLevel
        painLevelColor={painLevelColor}
        setPainLevelColor={setPainLevelColor}
      />
  );
};

当两者在同一个文件中时它工作正常但是当我将我的 child 组件移动到我的组件文件夹时它抛出这个错误 TypeError: undefined is not an object (evaluating '_ref.painLevelColor')

我没有设置项目的基础,也不知道是什么原因造成的

有什么想法吗?

您应该提供子实体的更多信息、导入和内容。

但是从你描述的错误和情况来看,我猜你应该是循环依赖。您可能已经在彼此中使用了父项和子项,这会导致导入的值未定义。当它们在同一个文件中时不会发生。

您可以在此处阅读有关循环依赖及其解决方法的更多信息:

https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de