import React from 'react';
import { connect } from 'react-redux';
import { add as addMan } from './../actions/men';
class ActionsReducers extends React.Component {
componentWillMount() {
console.log( 'ActionsReducers.componentWillMount' );
this.props.addMan( { name: 'Альберт', family: 'Эйнштейн' } );
}
shouldComponentUpdate( nextProps, nextState ) {
console.log( 'ActionsReducers.shouldComponentUpdate' );
console.log( nextProps, nextState );
}
componentWillReceiveProps( nextProps ) {
console.log( 'ActionsReducers.componentWillReceiveProps' );
console.log( nextProps );
}
moreMen = () => {
console.log( 'ActionsReducers.moreMen' );
this.props.addMan( { name: 'Томас', family: 'Эдисон' } );
}
render() {
console.log( 'ActionsReducers.render' );
console.log( this.props.men );
return (
<div>
<main>
<h1>Действия и редьюсеры</h1>
<div>
<div className='pure-g'>
<div className="pure-u-1-2">
<h2>Люди</h2>
<button onClick={this.moreMen}>Еще</button>
</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 );
TypeError: Cannot read property 'props' of undefined
import React from 'react';
import { connect } from 'react-redux';
import { add as addMan } from './../actions/men';
class ActionsReducers extends React.Component {
componentWillMount() {
console.log( 'ActionsReducers.componentWillMount' );
this.props.addMan( { name: 'Альберт', family: 'Эйнштейн' } );
}
moreMen() {
console.log( 'ActionsReducers.moreMen' );
this.props.addMan( { name: 'Томас', family: 'Эдисон' } );
}
render() {
console.log( this.props.men );
return (
<div>
<main>
<h1>Действия и редьюсеры</h1>
<div>
<div className='pure-g'>
<div className="pure-u-1-2">
<h2>Люди</h2>
<button onClick={this.moreMen}>Еще</button>
</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 );
import React from 'react';
import { connect } from 'react-redux';
import { add as addMan } from './../actions/men';
import { add as addAnimal } from './../actions/animals';
class ActionsReducers extends React.Component {
constructor() {
super();
console.log( store );
// store.dispatch(
//
// addMan( [
// { name: 'Альберт', family: 'Эйнштейн' },
// { name: 'Мария', family: 'Кюри' },
// { name: 'Томас', family: 'Эдисон' } ]
// )
//
// );
// store.dispatch(
//
// addAnimal( [
// { name: 'Лиса' },
// { name: 'Медведь', },
// { name: 'Волк' } ]
// )
//
// );
}
getMoreAnimals() {
// store.dispatch(
//
// addAnimal( [
// { name: 'Енот' },
// { name: 'Лось', },
// { name: 'Рысь' } ]
// )
//
// );
}
render() {
// let currentState = store.getState();
// { currentState.animalsReducers.animalsApp.map( function( animal ) { return animal.name + ', ' } ) }
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>
<button onClick={this.getMoreAnimals}>Еще</button>
</div>
</div>
</div>
</main>
</div>
);
}
}
function mapStateToProps( store ) {
return { store: store }
}
const mapDispatchToProps = {
addMan
};
export default connect( mapStateToProps, mapDispatchToProps )( ActionsReducers );
Экшн
Редюсер