简体   繁体   English

未捕获的错误:对象作为React子元素无效,但它是一个数组

[英]Uncaught Error: Objects are not valid as a React child but it's an array

I'm very new to react trying to learn it by building a small app which displays ingredients based on button clicks. 我是一个非常新的响应者,它试图通过构建一个小型应用程序来学习它,该应用程序可基于按钮单击来显示成分。

In my recipe view component I'm trying to render multiple ingredient views however I'm getting this error: 在我的配方视图组件中,我尝试呈现多个成分视图,但是出现此错误:

Uncaught Error : Objects are not valid as a React child (found: object with keys {ingred}). 未捕获的错误 :对象作为React子对象无效(找到:带有键{ingred}的对象)。 If you meant to render a collection of children, use an array instead. 如果要渲染子级集合,请改用数组。

My code is far from perfect, and it's difficult for me to try and explain the issue I can link you to my full application if you don't understand my explanation to see the error for yourself. 我的代码远非完美,如果您不理解我的解释以自己查看错误,那么我很难尝试解释这个问题,我可以将您链接到我的完整应用程序。 Here's the relevant code: 以下是相关代码:

 import React from 'react'; import IngredView from './IngredView' import './styles/RecipeView.css'; const RecipeView = ( {recipe} ) => { const { recipe_name, set_ingredients } = recipe const isIngredDefined = (set_ingredients !== undefined) const ingred = isIngredDefined ? set_ingredients.map(set_ingredients => { return ( <IngredView set_ingredients={set_ingredients} /> ); }) : "none" console.log(ingred); return ( <section className="recipe-view"> <h1 className="recipe-name">{recipe_name}</h1> {set_ingredients !== undefined && {ingred} } </section> ) } export default RecipeView; 

And here is my Ingredient View ( the console log was for debugging) 这是我的成分视图(控制台日志用于调试)

 import React from 'react'; import './styles/IngredView.css' const IngredView = ({set_ingredients}) => { const { components } = set_ingredients[0] console.log(set_ingredients); return <section className="ingred-view"><img className="ingred-image" alt="comp" src={components[0]} /><img className="ingred-image" alt="comp" src={components[1]} /><img className="ingred-image" alt="comp" src={components[2]} /><img className="ingred-image" alt="comp" src={components[3]} /><img className="ingred-image" alt="comp" src={components[4]} /></section> } export default IngredView; 

There are a couple things here that I can see right off the bat in your RecipeView component. 在这里,我可以在RecipeView组件中看到几件事。

First of all, in it's return statement, you have {set_ingredients !== undefined && {ingred} } . 首先,在return语句中,您有{set_ingredients !== undefined && {ingred} } The brackets surrounding ingred are incorrect. 周围的括号ingred是不正确的。 You are not allowed to have those brackets within them in such a manner in JS (well... You are technically allowed, but you are turning ingred into an object with ingred as an attribute, which I don't think was your intention and they cannot be rendered in React as the error being displayed to you suggests), so the correct statement would be {set_ingredients !== undefined && ingred } . 在JS中,您不允许以这种方式将这些括号包含在其中(嗯...从技术上来说是允许的,但是您正在将ingred变成具有ingred作为属性的对象,我认为这并不是您的意图,它们无法按照显示的错误提示在React中呈现),因此正确的语句应该是{set_ingredients !== undefined && ingred }

Secondly, still looking at that same statement. 其次,仍在看同样的陈述。 From what I can tell, your intention is to display ingred if set_ingredients is NOT undefined and nothing otherwise, is this correct? 据我所知,如果set_ingredients不是undefined ,那么您的意图是显示ingred ,否则,这是正确的吗? The way you are doing it is incorrect, since if the first condition is false , you will display false . 您的操作方式不正确,因为如果第一个条件为false ,则将显示false React does not render boolean values. React不呈现布尔值。 To fix this, I would use the ternary operator , which will make the statement look like this: 为了解决这个问题,我将使用三元运算符 ,它将使语句看起来像这样:

{set_ingredients !== undefined ? ingred : null }

In your IngredView , I am not sure what you are trying to do. 在您的IngredView ,我不确定您要做什么。 You set your components variable as the first element from set_ingredients . 您将components变量设置为set_ingredients的第一个元素。 This will only work if the object you are trying to deconstruct components from is from the first index of set_ingredients , which based off of the screenshot you showed me, is not the case. 仅当您尝试从中解构components的对象来自set_ingredients的第一个索引set_ingredients (基于您展示给我的屏幕截图),情况并非如此。 The correct way to do this, would simply be: 正确的方法是:

const { components } = set_ingredients

So to sum it all up: 因此,总结一下:

  • Remove the extra brackets from the return statements RecipeView . 从return语句RecipeView删除多余的括号。 The result should be this: 结果应该是这样的:

    {set_ingredients !== undefined && ingred } {set_ingredients!==未定义&& ingred}

  • Use the ternary operator instead of the && statement in the above code: 使用三元运算符代替上面的代码中的&&语句:

    {set_ingredients !== undefined ? {set_ingredients!==未定义? ingred : null } ingred:null}

  • In IngredView , pull components out of the set_ingredients object, not the first index of it: IngredView ,将components拉出set_ingredients对象,而不是它的第一个索引:

    const { components } = set_ingredients const {组件} = set_ingredients

暂无
暂无

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

相关问题 React:Uncaught Error:对象作为React子对象无效 - React: Uncaught Error: Objects are not valid as a React child 未捕获的错误:对象作为 React 子项无效 - Uncaught error: Objects are not valid as a React child 未捕获的错误:对象作为React子对象无效 - Uncaught Error: Objects are not valid as a React child 我收到此错误“未捕获错误:对象作为 React 子项无效”,即使我使用了对象数组怎么办 - I getting this error 'Uncaught Error: Objects are not valid as a React child' even if I used Array of objects what to do 过滤器 function 错误:: 未捕获错误:对象作为 React 子级无效....改用数组 - filter function error :: Uncaught Error: Objects are not valid as a React child .... use an array instead 间歇性的React Uncaught错误:对象无效作为React子级 - Intermittent React Uncaught Error: Objects are not valid as a React child 如何处理未捕获的错误:对象作为 React 子错误无效 - How to handle Uncaught Error: Objects are not valid as a React child error 如何调试此错误:未捕获(在promise中)错误:对象作为React子对象无效 - How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child 未捕获的错误:对象作为 React 子项无效,如果您打算呈现子项集合,请改用数组 - Uncaught Error: Objects are not valid as a React child , If you meant to render a collection of children, use an array instead 未捕获的错误:对象作为 React 子对象无效(找到:[object HTMLImageElement]) - Uncaught Error: Objects are not valid as a React child (found: [object HTMLImageElement])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM