简体   繁体   English

ReactJS / Javascript - 无法为对象中的项目渲染组件

[英]ReactJS / Javascript - Trouble Rendering Components for Items in Object

I'm having some trouble rendering components for each instance of an item in an object.我在为对象中的每个项目实例渲染组件时遇到了一些麻烦。

While I'm able to log the individual titles of each item, the return function doesn't return anything, regardless of which component I try to return.虽然我能够记录每个项目的单独标题,但无论我尝试返回哪个组件,返回函数都不会返回任何内容。 There are no errors, apparently.显然没有错误。

Is there perhaps a better way of returning components according to each item in an object?是否有更好的方法根据对象中的每个项目返回组件?

Any help would be greatly appreciated!任何帮助将不胜感激! :) :)

 import React, { Component } from 'react'; export default class Wrapper extends Component { const obj = () => ({ "one": { "title": "one", "description": "foo", }, "two": { "title": "two", "description": "bar", }, }); renderSingleItem(instance) { console.log(instance); // THIS WORKS JUST FINE! return ( // THIS DOESN'T WORK?! <h2 key={instance.title}> {instance.description} </h2> ); } renderAllItems(data) { Object.entries(data).forEach(([key, instance]) => { return this.renderSingleItem(instance); }); } render() { return ( <div> {this.renderAllItems(this.obj)} </div> ); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

I've also attempted the following method, which actually renders a component, but only for the first item in the object.我还尝试了以下方法,该方法实际上渲染了一个组件,但仅针对对象中的第一项。

 import React, { Component } from 'react'; export default class Wrapper extends Component { const obj = () => ({ "one": { "title": "one", "description": "foo", }, "two": { "title": "two", "description": "bar", }, }); renderSingleItem(instance) { console.log(instance); return ( <h2 key={instance.title}> {instance.description} </h2> ); } renderAllItems(data) { for (var key in data) { if (data.hasOwnProperty(key)) { var instance = data[key]; for (var prop in instance) { if (instance.hasOwnProperty(prop)) { return (this.renderSingleItem(instance)); } } } } } render() { return ( <div> {this.renderAllItems(this.obj)} </div> ); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

FYI, in my project, I'm importing a JSON object.仅供参考,在我的项目中,我正在导入一个 JSON 对象。

You have 2 issues in this function.您在此功能中有 2 个问题。

renderAllItems(data) {
        Object.entries(data).forEach(([key, instance]) => {
            return this.renderSingleItem(instance);
        });
    }

You need to add another return before Object.keys and you should be using .map and not .forEach since forEach is void, meaning it wont return anything.您需要在Object.keys之前添加另一个 return 并且您应该使用.map而不是.forEach因为forEach是无效的,这意味着它不会返回任何内容。

The code should look like this.代码应该是这样的。

renderAllItems(data) {
       return Object.entries(data).map(([key, instance]) => {
            return this.renderSingleItem(instance);
        });
    }

This solution worked great for me:这个解决方案对我很有用:

 import React from 'react'; import { render } from 'react-dom'; export default class Wrapper extends React.Component { constructor(props) { super(props) this.obj = { "one": { "title": "one", "description": "foo", }, "two": { "title": "two", "description": "bar", }, }; } renderSingleItem(instance, k) { console.log(instance); // THIS WORKS JUST FINE! return (<h2 key={k} children={instance.description} />); } /* * Not necessary renderAllItems(data) { Object.entries(data).forEach(([key, instance]) => { return this.renderSingleItem(instance); }); }*/ render() { return ( <div> {Object.keys(this.obj).map((k) => { return this.renderSingleItem(this.obj[k], k); })} </div> ); } } // I'll leave this for you // render(<Wrapper />, document.getElementById('root'));

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

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