2016-11-24 101 views
0

的数据是这种形式:错误而呈现JSON数据[Reactjs]

this.props.tagsview

_getTags() { 
 
    console.log(this.props.tagsview); 
 
    return this.props.tagsview.map(tags => { 
 
     var newItem = tags.map(p => <span className="tag" key={p.tag_id}>{p.name}</span>); 
 
     return ({newItem); 
 
    }); 
 
}

所以,当我跑这我越来越:

error react.js:18893 Uncaught Error: Objects are not valid as a React child (found: object with keys {newItem}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.(…)

我错过了什么?
是否有其他解决方法?

回答

0

这里有两个问题。其中一个是你正在返回一个嵌套列表 [[]]我想你可能只想返回一个列表[]。 你也从你的外部地图函数返回{newItem}。 这不是React可以呈现的东西。 React需要反应元素或反应元素列表。

做你的意思是要做到:

return this.props.tagsview.map(tags => { 
     return tags.map(p => <span className="tag" key={p.tag_id}>{p.name}</span>); 
    }); 

或可能

this.props.tagsview 
    .reduce((tags, view) => [...tags, ...view.map(t =><span className="tag" key={p.tag_id}>{p.name}</span>)], [])