Answer the question
In order to leave comments, you need to log in
How to re-render content after changing state value?
import React, { Component } from 'react'
import Items from '../items'
import Filters from '../filters'
import Service from '../../services'
export default class Shop extends Component {
KubicService = new Service()
state = {
items: {},
filters: {},
filtered: 'all'
}
componentDidMount() {
this.KubicService.getCategories()
.then((result) => {
this.setState({
filters: result.data
})
})
this.KubicService.postCatalog()
.then((result) => {
this.setState({
items: result.data
})
})
}
onFilterClick = (e) => {
const cats = {...this.state.filters}
const itemId = e.target.id
console.log(typeof this.state.items)
if(itemId == 'all') {
this.setState({
filtered: 'all'
})
} else {
this.setState({
filtered: cats[itemId].id
})
}
}
render(){
return(
<div>
<Filters categories={this.state.filters} onFilterClick = { this.onFilterClick }/>
<Items items = { this.state.items } />
</div>
)
}
}
import React from 'react'
const Items = (props) => {
const arr = Array.from(props.items)
const elements = arr.map((item, index) => {
let url = ''
if(item && item.preview_image) {
url = 'http://******'
url = url + item.preview_image
}
return (
<div key={index} className='store-item'>
<img className='store-item__img' src={url} alt='item img'></img>
<h3 className='store-item__title'>{item.name}</h3>
<h3 className='store-item_price'>${item.price}</h3>
</div>
)
})
return (
<div className='store-items'>
{ elements }
</div>
)
}
export default Items
import React from 'react'
const Filters = (props) => {
const arr = Array.from(props.categories)
const filters = arr.map((item, index) => {
return(
<button className='category btn' key={index} id={index} onClick={ props.onFilterClick }>{item.name}</button>
)
})
return(
<div className='store-filters'>
<div className='filter-category'>
<div className='categories'>
<button className='category-all btn' id="all" onClick={ props.onFilterClick }>all</button>
{ filters }
</div>
<select className="filter">
<option className='filter-variable'>Price: high to low</option>
<option className='filter-variable'>Price: low to high</option>
</select>
</div>
</div>
)
}
export default Filters
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question