console.log( this.props.men ); // Изначально UNDEFINED
this.props.addMan( [ { name: 'Альберт', family: 'Эйнштейн' } ] ); // Запись в хранилище
console.log( this.props.men ); // Все равно UNDEFINED
import React from 'react';
import { connect } from 'react-redux';
import { add as addMan } from './../actions/men';
class ActionsReducers extends React.Component {
constructor( props ) {
super( props );
}
componentWillReceiveProps( nextProps ) {
console.log( nextProps );
}
render() {
console.log( this.props.men );
this.props.addMan( [ { name: 'Альберт', family: 'Эйнштейн' } ] );
console.log( this.props.men );
return (
<div>
<main>
<h1>Действия и редьюсеры</h1>
<div>
<div className='pure-g'>
<div className="pure-u-1-2">
<h2>Люди</h2>
</div>
<div className="pure-u-1-2">
<h2>Животные</h2>
</div>
</div>
</div>
</main>
</div>
);
}
}
function mapStateToProps( store ) {
return {
men: store.men,
}
}
const mapDispatchToProps = {
addMan
};
export default connect( mapStateToProps, mapDispatchToProps )( ActionsReducers );
class ActionsReducers extends React.Component {
handleAddMan = () => {
this.props.addMan({ name: 'Альберт', family: 'Эйнштейн' });
};
render() {
const { men } = this.props;
return (
<Wrapper>
<List>
{men.map(man => <Man key={man.id} man={man} />)}
{!men.length && <Empty>No data in list.</Empty>}
</List>
<Button onClick={this.handleAddMan}>Add man</Button>
</Wrapper>
);
}
}
class ActionsReducers extends React.Component {
componentDidMount() {
this.props.addMan({ name: 'Альберт', family: 'Эйнштейн' });
}
render() {
const { men } = this.props;
return (
<Wrapper>
<List>
{men.map(man => <Man key={man.id} man={man} />)}
{!men.length && <Empty>No data in list.</Empty>}
</List>
</Wrapper>
);
}
}