I have a component that is a <select>
that shows all the categories, and I would need to make it work, let's say with a reagent to be able to filter the demos by categories.
This is the select component
import React from 'react';
class Category extends React.Component {
constructor() {
super();
this.state={items:[]};
}
componentDidMount() {
fetch('http://localhost:3000/api/categories')
.then(result => result.json())
.then(items => {this.setState({items});
});
}
render() {
return (
<div>
<center>
<select>
<option default> ---- Select Category ---- </option>
{this.state.items.length ?
this.state.items.map(item=>
<option>{item.name}</option>
)
: <li>Loading...</li>
}
</select>
</center>
</div>
);
}
}
export default Category;
And this one of the content to filter
import React from 'react';
var imgdir = '../../../assets/images/';
class Content extends React.Component {
constructor() {
super();
this.state={items:[]};
}
componentDidMount(){
fetch('http://localhost:3000/api/demos')
.then(result=>result.json())
.then(items=>this.setState({items}))
}
render() {
return(
<ul>
{this.state.items.length ?
this.state.items.map(item=>
<div id="1" className="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div className="hovereffect">
<img className="img-responsive" src={imgdir + item.img} alt={item.name} />
<div className="overlay">
<h2>{item.name}</h2>
<p>
<b>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b>
</p>
<p>
<a href="#">VIEW DEMO</a>
</p>
</div>
</div>
</div>
)
: <li>Loading...</li>
}
</ul>
)
}
}
export default Content;
Each category has an id in its database, and each div as you see has an id that in this case I assigned it manually because I still did not do the part to get that id from the DB but skipping this, I would like to know how to make this filtering work.
PS: the id of the category can be obtained using {item.id}