React 中的渲染对象属性

IT技术 javascript reactjs object
2021-01-15 03:35:22

我有一个这样的对象

export const otherInformation = [
{
    "FAQ": ['Getting started guide', 'Selling policy'],
    "Help & Support": ['Help guide', 'Selling policy'],
    "Legal": ['Terms of Use', 'Privacy Policy']
}]

我的代码

class Information extends Component {
    render() {
        const otherInformationLoop = otherInformation.map((value, key) => {
            return (
                <div>
                    <div className="col-md-4" key={key}>
                        <div className="dashboard-info">

                            {Object.keys(value).map((val, k) => {
                                return (<h4 k={k}>{val}</h4>)
                                })
                            }

                        </div>
                    </div>
                </div>
            )
        })

        return (
            { otherInformationLoop }
            // <div></div>
        );
    }
}

我在遍历对象时遇到问题。

得到的错误是这样的

Information.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object

如何遍历对象以获得获得的结果

提前致谢。任何帮助表示赞赏

1个回答

您正在渲染一个数组,但您只能从您的react组件返回一个块,将您的地图功能包装在一个 div 中

class Information extends Component {
    render() {
        const otherInformationLoop = otherInformation.map((value, key) => {
            return (
                <div>
                    <div className="col-md-4" key={key}>
                        <div className="dashboard-info">

                            {Object.keys(value).map((val, k) => {
                                return (<h4 k={k}>{val}</h4>)
                                })
                            }

                        </div>
                    </div>
                </div>
            )
        })

        return (

            <div>{ otherInformationLoop }</div>
        );
    }
}
实际上,render 方法返回了一个具有单个 otherInformationLoop 属性的对象。只返回该值,没有任何类型的括号就可以在 React 16+ 中工作(但每个外部 div 上都需要一个 key 属性)。
2021-04-03 03:35:22