我试图创建一个数组来存储我的分类链接,然后显示它们,但是我没有在我的DOM中显示任何内容。任何帮助,将不胜感激:)从Array React创建一个列表JS
import React from "react";
import { SidebarCategory } from './SidebarCategory';
class SidebarCategories extends React.Component {
constructor() {
super();
this.state = {
categories: []
}
}
componentWillMount() {
this.setState({categories: [
{
id: 1,
icon: "icon",
title: "Home",
},
{
id: 2,
icon: "icon",
title: "Gallery",
}
]});
}
render() {
return (
<ul className="sidebar__categories container-fluid">
{this.state.categories.map(category => {
return (
<SidebarCategory key={category.id} title={category.title} />
)
})};
</ul>
);
}
}
export default SidebarCategories;
编辑:
控制台错误:
bundle.js:357警告:React.createElement:类型无效 - 预期字符串(内置在组件中)或类/函数(用于复合组件),但得到:未定义。您可能忘记从您定义的文件中导出组件。检查Sidebar
的渲染方法。 在侧栏(由应用程序创建的) 在DIV(由应用程序创建的) 在应用
sidebarCategory.js
import React from "react";
export class SidebarCategory extends React.Component {
render() {
const SidebarCategory = ({ title }) => {
return (<div className="sidebarCategory">{title}</div>);
}
}
}
sidebar.js:
import React from "react";
import { SidebarCategories } from "./SidebarCategories";
export class Sidebar extends React.Component {
render() {
return (
<div className="sidebar col-sm-2">
<div className="row">
<div className="sidebar__header col">
<img alt="Logo" className="img-fluid sidebar__header__logo" src="../resources/img/logo-white.png" />
{'\u00A0'} <h4 className="i-block">Title</h4>
</div>
</div>
<div className="row">
<div className="sidebar__user container-fluid">
<div className="row">
<div className="col-sm-4">
<img alt="User DP" className="sidebar__user__img img-fluid rounded-circle" src="http://via.placeholder.com/100x100" />
</div>
<div className="col-sm-8">
<p><strong>Welcome</strong><br />
Mark Hughes</p>
</div>
</div>
</div>
</div>
<div className="row">
<SidebarCategories />
</div>
</div>
);
}
}
index.js:
import React from "react";
import { render } from "react-dom";
import Sidebar from "./components/sidebar";
import Content from "./components/content";
class App extends React.Component {
render() {
return (
<div className="row">
<Sidebar />
<Content />
</div>
);
}
}
render(<App className="container-fluid"/>, window.document.getElementById("app"));
您是否收到任何控制台错误? –
bundle.js:357警告:React.createElement:type无效 - 预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记将组件从其定义的文件中导出。检查“Sidebar”的渲染方法。 in Sidebar(由App创建) in div(由App创建) in App – MarkHughes88
@ MarkHughes88您在哪里使用循环创建元素,还可以显示“SidebarCategory”组件? –