简体   繁体   English

反映渲染错误

[英]React render error

I'm receiving this error when I try to render my nested nav: 我尝试渲染嵌套导航时收到此错误:

Objects are not valid as a React child (found: object with keys {id, name}). 对象作为React子对象无效(找到:具有键{id,name}的对象)。 If you meant to render a collection of children, use an array instead. 如果您要渲染子集合,请使用数组。

I'm not sure how to go about rendering my mapped nav correctly. 我不确定如何正确渲染我的映射导航。 Any help would be appreciated. 任何帮助,将不胜感激。

let items = [
  { id: 0, name: 'Products', items: [{ id: 2, name: 'M-PET.NET' }, { id: 3, name: 'M-PET.WEB' }]},
  { id: 4, name: 'Services', items: [{ id: 5, name: 'M-PET Hosted' }, { id: 6, name: 'Custom Programming' }]},
  { id: 7, name: 'TurnKey', items: [{ id: 8, name: 'About' }, { id: 9, name: 'Project Management' }, { id: 10, name: 'IT Tasks' }, { id: 11, name: 'Maintenance' }]},
  { id: 12, name: 'Key Industries', items: [{ id: 13, name: 'Transportation' }, { id: 14, name: 'Hospitality' }]},
  { id: 15, name: 'Company', items: ['Team', 'History']},
]

class Nav extends Component {
  render() {
    let nav = items.map((item) => <li node={item} children={item.items} key={item.id}></li>)
    console.log(nav)
    return (
      <div>
        <ul className='nav'>
          {nav}
        </ul>
      </div>
    )
  }
}

export default Nav

put items in the state items放在州里

state = {
 items : [...] //array
}

and then map it as 然后将其映射为

let nav = this.state.items...

Or if you don't put it in state you can access it with the context 或者,如果您没有将其置于状态,则可以使用context访问它

let nav = this.items...

The point to note here is to use items in the same class which is Nav in your case by importing the source or define it in the same class. 这里要注意的一点是要使用items在同一类,这是Nav通过导入源的情况下,或在同一类中定义它。

class Nav extends Component {

 let items = [...];
 ...
 render(){
  let nav = this.items...
 }
}

or 要么

class Nav extends Component {

 state = { items:[...] };
 ...
 render(){
  let nav = this.state.items...
 }
}

You've error in this line of code: 你在这行代码中出错了:

<li node={item} children={item.items} key={item.id}>
// passed as object ^^ (item.items)

So, removing this will work fine. 所以,删除它将工作正常。 What you'll do with it like this: 你会用这样做:

<li node={item} key={item.id}>{renderListItems(item.items)}</li>

Now, you've different renderListItems component in which you can map to iterate through children: 现在,您有不同的renderListItems组件,您可以在其中映射以迭代子项:

renderListItems = (items) => {
     return items.map(item => (
       <p key={`don't-forget-the-key-to-unique`}>{item.name}</p>
     ))
   }

You can't just return an array of objects because there's nothing telling React how to render that. 你不能只返回一个对象数组,因为没有什么可以告诉React如何渲染它。 You'll need to return an array of components or elements like: 您需要返回一组组件或元素,如:

let nav = items.map((item) =>
  <li
    node={item}
    children={item.items.map(x => x.name)}
    key={item.id}>
  </li>)

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

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