简体   繁体   English

错误:对象作为 React 子对象无效(在 Promise.all 内)

[英]Error: Objects are not valid as a React child (within Promise.all)

I am creating React practice project and I am still dealing with this kind of error (Objects are not valid as a React child).我正在创建 React 实践项目,但我仍在处理这种错误(对象作为 React 子对象无效)。 More in screenshot below.更多在下面的屏幕截图中。

Thrown error抛出的错误

You can see problem part of code in that screenshot too .您也可以在该屏幕截图中看到代码的问题部分

Some explanation: This piece of code is a part inside an useEffect ( this useEffect content is executed only if fixturesIds aren't undefined) .一些解释:这段代码是useEffect中的一部分只有在未定义fixturesIds时才会执行此 useEffect 内容) Then some get requests are pushed into an array predictionList and they are resolved together in Promise.all .然后一些get 请求被推送到数组predictionList中,并在Promise.all中一起解析。 Promise.all returns an array which I map to get required data. Promise.all 返回一个数组,我用 map 获取所需的数据。 This all ( prediction.value.data ) is setted as a state ( setFixturesDetails ).这一切( prediction.value.data设置为 statesetFixturesDetails )。

Prediction.value.data looks like: [{..}, {..}, {..}, {..}] Prediction.value.data看起来像:[{..}, {..}, {..}, {..}]

It worked very well, when I logged it into console.当我将它登录到控制台时,它工作得很好。

But when I tried to use this array of objects as a props in Component (see below)...但是当我尝试使用这个对象数组作为组件中的道具时(见下文)......

return (
    <div className='App'>
      {fixturesDetails.length > 0 ? (
        fixturesDetails.map((details) => (
          <Prediction
            homeTeam={details.teams.home.name}
            awayTeam={details.teams.away.name}
            winner={details.predictions.winner}
          />
        ))
      ) : (
        <h2>Loading</h2>
      )}
    </div>

...it has started to throw a mentioned Error . ...它已经开始抛出一个提到的 Error

Loading appears normally and it crashes when fixturesDetails are setted ( fixturesDetails.length > 0 is true),加载正常出现,并在设置fixturesDetails 时崩溃fixturesDetails.length > 0 为真),

so instead of showing Prediction component it shows that error.因此,它没有显示预测组件,而是显示了该错误。

Any ideas why does it throw that error?任何想法为什么会引发该错误? I researched a lot but I am still not able to figure it out.我研究了很多,但我仍然无法弄清楚。

Thanks!谢谢!

Well, I have just figured it out.好吧,我刚刚想通了。

The problem is that it is not possible to use object as a props in JSX syntax.问题是不可能在 JSX 语法中使用 object 作为道具。

I logged all my props in the console and found out that one of my props is really an object.我在控制台中记录了我的所有道具,发现我的一个道具确实是 object。

Solution is pretty clear, just choose right property inside an object.解决方案很清楚,只需在 object 中选择正确的属性即可。 If there is complicated API with nested objects it can be very easy to overlook you are actually returning an object within prop.如果存在带有嵌套对象的复杂 API 很容易忽略您实际上是在 prop 中返回 object。

Maybe it will help someone someday:)也许有一天它会帮助某人:)

暂无
暂无

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

相关问题 使用Promise.all在Google Maps上放置标记-对象作为React子对象无效(发现:[object Promise]) - Using Promise.all to place Markers on Google Maps - Objects are not valid as a React child (found: [object Promise]) 在 promise.all(响应)之后设置挂钩时,对象作为 React 子项无效? - Objects are not valid as a React child while setting hook after promise.all(responses)? React 错误“对象作为 React 子项无效(找到:[object Promise])” - React error "Objects are not valid as a React child (found: [object Promise])" 在Promise.all反应中获取错误 - Getting an error using Promise.all in react 如何调试此错误:未捕获(在promise中)错误:对象作为React子对象无效 - How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child 错误:对象在 reactjs 中作为 React 子对象无效(找到:[object Promise]) - Error: Objects are not valid as a React child (found: [object Promise]) in reactjs 我收到一个错误:对象作为 React 子对象无效(找到:[object Promise]) - I got an Error: Objects are not valid as a React child (found: [object Promise]) 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) Promise.all用于JavaScript中的对象 - Promise.all for objects in Javascript 对象作为 React 子级无效(发现:[object Promise]) - Objects are not valid as a React child (found: [object Promise])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM