Using Redux with Components
Counter Example
import React from 'react';
import { connect } from 'react-redux';
import { increase, decrease } from '../reducers/counter';
function mapStateToProps(state) {
return {
counter: state.counter,
};
}
function mapDispatchToProps(dispatch) {
return {
onIncrease: () => dispatch(increase()),
onDecrease: () => dispatch(decrease()),
};
}
const Counter = ({ onIncrease, onDecrease, counter }) => {
return (
<div>
<pre>{ counter }</pre>
<button onClick={ onIncrease }>Increase</button>
<button onClick={ onDecrease }>Decrease</button>
</div>
);
};
const App = connect(
mapStateToProps,
mapDispatchToProps,
)(Counter);
ReactDOM.render(
App,
document.getElementById('root')
);Full Example
Last updated
Was this helpful?