简体   繁体   English

反应(本机)加载子组件错误

[英]React (Native) loading component error on children

I have a written a loading component for my React Native App that shows a loading screen as long as the data is not retrieved.我为我的 React Native App 编写了一个加载组件,只要未检索到数据,它就会显示加载屏幕。 This looks something like this:这看起来像这样:

function LoadingChecker({ children, data }) {
  const notLoaded = data.filter((item) => !isLoaded(item));

  if (notLoaded.length > 0) {
    return (
      <View style={[styles.infoView, styles.centered]}>
        <MaterialCommunityIcons name="information-outline" color={Colors.grey} size={45} />
        <Text>Loading ...</Text>
      </View>
    );
  }

  return (
    { children }
  );
}
  return (
    <LoadingChecker data={[information]}>
      <View style={styles.mainView}>
        <Text>information.name</Text>
      </View>
    </LoadingChecker>
  );

The problem is that I get an exception indicating that information.name is undefined.问题是我得到一个异常,表明information.name未定义。

It seems to me that even tough I conditionally render the Text View, React is still preparing or evaluting it and because my information is undefined it fails.在我看来,即使我有条件地渲染文本视图,React 仍在准备或评估它,并且因为我的信息未定义它失败了。 If I replace information.name with information?.name it works and I see the loading screen, followed by the information screen.如果我将information.name替换为information?.name它可以工作,我会看到加载屏幕,然后是信息屏幕。

I'm I doing something wrong?我做错了什么? Could I tell React not to evalute the children?我可以告诉 React 不要评估孩子吗?

Since you are not doing anything with children in LoadingChecker , I don't really see the point of having LoadingChecker encapsulate everything to be honest.由于您没有对LoadingChecker中的children做任何事情,因此我真的不明白让LoadingChecker封装所有内容的意义。

I would do something like this instead:我会做这样的事情:

function Loading() {
  return (
    <View style={[styles.infoView, styles.centered]}>
      <MaterialCommunityIcons name="information-outline" color={Colors.grey} size={45} />
      <Text>Loading ...</Text>
    </View>
  );
}
return !isLoaded(information) ? (
  <Loading />
) : (
  <View style={styles.mainView}>
    <Text>{information.name}</Text>
  </View>
)

Otherwise if you want to keep your current structure, I don't think you could do anything better than information?.name like you suggested youself.否则,如果您想保持当前的结构,我认为您没有比information?.name就像您自己建议的那样。

It seems to me that even tough I conditionally render the Text View, React is still preparing or evaluting it and because my information is undefined it fails在我看来,即使我有条件地渲染文本视图,React 仍在准备或评估它,因为我的信息未定义,所以它失败了

No, you're wrong here.不,你错了。 If you're conditionally rendering some children, React will not prepare (there's no concept like that in React though) them on its own.如果你有条件地渲染一些孩子,React 不会自行准备(尽管 React 中没有类似的概念)它们。 To prove this issue I created a sandboxhere , where I conditionally rendering a child component that uses an updated state value.为了证明这个问题,我在这里创建了一个沙箱,我在其中有条件地渲染了一个使用更新的 state 值的子组件。 As long as the state is not updated the parent component, LoadingChecker , will not render its child component, <p>Loaded done. {data.message}</p>只要 state 没有更新,父组件LoadingChecker就不会渲染其子组件, <p>Loaded done. {data.message}</p> <p>Loaded done. {data.message}</p> not it shows any error for {data.message} rather it'll show 'loading' text. <p>Loaded done. {data.message}</p>不会显示{data.message}的任何错误,而是显示“正在加载”文本。 So, I think here you've some different issues with the shape of the data you're fetching.因此,我认为您在获取的数据的形状方面存在一些不同的问题。

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

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