<div id="root"></div>
const { createStore, applyMiddleware } = Redux;
const { Provider, connect } = ReactRedux
const thunk = ReduxThunk.default
const Increment = () =>{
return function (dispatch){
setTimeout(()=>{
dispatch({type: 'INCREMENT'})
},2000)
}
// return {
// type: 'INCREMENT'
// }
}
class Counter extends React.Component {
render() {
console.log(this.props);
return (
<div>
<h2>Counter</h2>
<div>
<button onClick={this.props.decrement}>-</button>
<span>{this.props.count}</span>
<button onClick={this.props.increment}>+</button>
</div>
</div>
)
}
}
const actionsCreators = (dispatch)=> {
return{
increment: () => {
//store.dispatch({ type: 'INCREMENT' });
dispatch(Increment())
},
decrement : () => {
dispatch({ type: 'DECREMENT' });
}
}
}
function mapStateToProps(state) {
console.log(state);
return {
count: state.count
};
}
const Concounter = connect(mapStateToProps,actionsCreators)(Counter);
const initialState = {
count: 0
};
function reducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
return {
count: state.count + 1
};
case 'DECREMENT':
return {
count: state.count - 1
};
default:
return state;
}
}
const store = createStore(reducer,window.devToolsExtension ? window.devToolsExtension() : undefined ,applyMiddleware(thunk));
const App = () => (
<Provider store={store}>
<Concounter/>
</Provider>
);
ReactDOM.render(<App />, document.getElementById('root'));
View Compiled
This Pen doesn't use any external CSS resources.