/category/:id
и пустой, в случае перехода по корневому пути /
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route component={Layout}>
<Route path='/' component={Phones}/>
<Route path='/categories/:id' component={Phones}/>
</Route>
<Route path='/phones/:id' component={Phone}/>
</Router>
</Provider>,
document.getElementById('root')
);
const Categories = ({categories, activeCategoryId}) => {
const renderCategory = (category, index) => {
const getActiveState = R.propEq('id', activeCategoryId)
const linkClass = classNames({
'list-group-item': true,
'active': getActiveState(category)
})
return (
<Link
to={`/categories/${category.id}`}
className={linkClass}
key={index}
>
{category.name}
</Link>
)
}
const renderAllCategory = () => {
const linkClass = classNames({
'list-group-item': true,
'active': R.isNil(activeCategoryId)
})
return (
<Link
to='/'
className={linkClass}
>
All
</Link>
)
}
return (
<div className='well'>
<h4>Brand</h4>
<div className='list-group'>
{renderAllCategory()}
{categories.map((category, index) => renderCategory(category, index))}
</div>
</div>
)
}
const mapStateToProps = (state, ownProps) => ({
categories: getCategories(state),
activeCategoryId: getActiveCategoryId(ownProps)
})
export default compose(
withRouter,
connect(mapStateToProps, null)
)(Categories)
const mapStateToProps = (state, ownProps) => ({
categories: getCategories(state),
activeCategoryId: getActiveCategoryId(ownProps)
})
const Phones = props => {
props.fetchPhones();
props.fetchCategories();
const {phones} = props;
return (
<div className="container-phones">
{phones.map((phone, index) => renderPhone(phone, index))}
</div>
);
};
{phones
.filter(phone => phone.categoryId === props.match.params.id)
.map((phone, index) => renderPhone(phone, index))
}
const mapStateToProps = (state, ownProps) => {console.log(ownProps); return({
categories: getCategories(state),
activeCategoryId: getActiveCategoryId(ownProps)
})}
export const getActiveCategoryId = ownProps => R.path(['params', 'id'], ownProps)
import React from 'react';
import {connect} from 'react-redux';
import {compose} from 'redux';
import {NavLink, withRouter} from 'react-router-dom';
import PropTypes from 'prop-types';
const Categories = ({categories}) => {
console.log(categories);
return (
<div className="categories-wrap">
<div className="">categories</div>
<ul className="categories-list">
{categories &&
categories.map((category, index) => {
if (index === 1) {
return (
<li key={index}>
<NavLink
activeClassName="active"
exact
to={`/category/${category.id}`}
>
{category.name}
</NavLink>
</li>
);
}
return (
<li key={index}>
<NavLink to={`/category/${category.id}`}>
{category.name}
</NavLink>
</li>
);
})}
</ul>
</div>
);
};
const mapStateToProps = state => ({
categories: state.categories,
});
export default compose(
withRouter,
connect(
mapStateToProps,
null,
),
)(Categories);
Categories.propTypes = {
categories: PropTypes.array,
props: PropTypes.object,
};
import React from 'react';
import {connect} from 'react-redux';
import PropTypes from 'prop-types';
import {fetchPhones, fetchCategories} from '../actions';
import {renderPhone} from './renderPhone';
const Phones = props => {
props.fetchPhones();
props.fetchCategories();
const {phones} = props;
return (
<div className="container-phones">
{phones.map((phone, index) => renderPhone(phone, index))}
</div>
);
};
const mapStateToProps = (state, ownProps) => {
console.log(ownProps);
return {
phones: state.phones,
};
};
const mapDispatchToProps = {
fetchPhones,
fetchCategories,
};
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Phones);
Phones.propTypes = {
fetchCategories: PropTypes.func,
fetchPhones: PropTypes.func,
phones: PropTypes.array,
};