简体   繁体   English

如何修复错误对象是无效的反应子?

[英]How to fix error objects are not valid react child?

I am trying map through an object with the goal to render the key and their value inside ap tag.我正在尝试通过一个对象映射,目的是在 ap 标记内呈现键及其值。 I am having the error message object are not valid react child.我有错误消息对象是无效的反应子。

How can I overcome this error?我该如何克服这个错误?

        <div className="d-flex flex-wrap">
              {
              
              Object.keys(features).map((item,index) => {           
                  console.log('type',item);
                  console.log(features[item]);
                  

                   return   <p key={item} className="fw-bold bg-light fs-6 text-primary m-1 p-2">{{[item]:features[item]}}</p> 
                      
               
              })
              }
          </div>

This error is because the code这个错误是因为代码

{{[item]:features[item]}}

actually results to an object.实际上结果是一个对象。 So child of <p> tag is an object.所以<p>标签的子元素是一个对象。 You can solve it by using Template literals inside <p> tag您可以通过在<p>标记中使用模板文字来解决它

 <div className="d-flex flex-wrap">
          {
         
          Object.keys(features).map((item,index) => {           
              console.log({[item]:features[item]});
              console.log(features[item]);
              

               return   <p key={item} className="fw-bold bg-light fs-6 text-primary m-1 p-2" >{`{${item}: ${features[item]}}`}</p> 
                  
           
          })
          }
      </div>

In this section of React doc , it is said that:在 React doc的这一部分中,据说:

You can put any valid JavaScript expression inside the curly braces in JSX您可以将任何有效的 JavaScript 表达式放在 JSX 中的花括号内

Moreover {[item]:features[item]} itself is not a valid expression, according to this list此外,根据此列表{[item]:features[item]}本身不是有效的表达式

So instead, you have to embed 2 expressions, item and features[item]因此,您必须嵌入 2 个表达式, itemfeatures[item]

return (
  <p key={item} className="fw-bold bg-light fs-6 text-primary m-1 p-2">
    {item}: {features[item]}
  </p>
)

It is because of the double brackets in your code这是因为您的代码中的双括号

Use only one brackets like this像这样只使用一个括号

{[item]:features[item]}

Following is JSON以下是JSON

{[item]:features[item]}

If you meant to render the JSON as a string then do like this.如果您打算将 JSON 呈现为字符串,请这样做。

JSON.stringify({[item]:features[item]})

Do it like this像这样做

Object.keys(features).map((item) => {   
return <p>  {item} : {features[item]}</p>
}

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

相关问题 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) 如何修复对象作为React子对象无效 - How to fix Objects are not valid as a React child 如何修复“对象作为 React 子对象无效” - How to fix 'Objects are not valid as a React child' 如何修复错误:对象作为 React 子对象无效。 如果您打算渲染一组孩子,请改用数组 - How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 如何修复“对象作为 React 子对象无效(找到:带有键 {} 的对象)” - How to fix "Objects are not valid as a React child (found: object with keys {}) 如何修复对象作为 React 子级无效 object promise - how to fix Objects are not valid as a React child object promise 对象无效为React子错误 - Objects are not valid as a React child error 在对象上循环 // 错误:对象作为 React 子对象无效 - Looping on objects // Error: Objects are not valid as a React child 如何调试此错误:未捕获(在promise中)错误:对象作为React子对象无效 - How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child 如何处理未捕获的错误:对象作为 React 子错误无效 - How to handle Uncaught Error: Objects are not valid as a React child error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM