Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  108] [ 1]  / answers: 1 / hits: 33836  / 7 Years ago, mon, august 14, 2017, 12:00:00

Im trying to create an array to store my category links and then display them however I'm getting nothing displaying in my DOM. Any help would be appreciated :)



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;


edit:



console error:



bundle.js:357 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of Sidebar.
in Sidebar (created by App)
in div (created by App)
in App



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));

More From » arrays

 Answers
0
import React from react;
import SidebarCategory from './SidebarCategory';

class SidebarCategories extends React.Component {
constructor() {
super();
this.state = {
categories: []
}
}

componentWillMount() {
this.setState({categories: [
{
id: 1,
title: Home,
},
{
id: 2,
title: Gallery,
}
]});
}

render() {
return (
<ul className=sidebar__categories container-fluid>
<SidebarCategory categories={this.state.categories} />
</ul>
);
}
}

export default SidebarCategories;


And add this in your SidebarCategory file:-



import React from react;

export class SidebarCategory extends React.Component {
render() {
return (
<li className=sidebar__category container-fluid>
<div className=row>
<div className=col-sm-10>
{this.props.categories.map((category, key) =>(
<span key={key}>{category.title}</span>
))}
</div>
</div>
</li>
);
}
}

[#56777] Thursday, August 10, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;