我試圖創建一個數組來存儲我的分類鏈接,然後顯示它們,但是我沒有在我的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」組件? –