Last active
October 31, 2019 09:46
-
-
Save renren89/c03ee352d75dad4a931d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render } from 'react-dom'; | |
import React, { Component } from 'react'; | |
import { createStore } from 'redux'; | |
import { connect, Provider } from 'react-redux'; | |
const reducer = (state = 0, action) => { | |
console.log('current state is ', state, 'and action is', action); | |
switch (action.type) { | |
case 'INCREASE': | |
return state + 1; | |
case 'DECREASE': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
const store = createStore(reducer); | |
const incrementAction = () => { | |
return { | |
type: 'INCREASE' | |
} | |
} | |
const decrementAction = () => { | |
return { | |
type: 'DECREASE' | |
} | |
} | |
@connect((state) => { | |
return { | |
counter: state | |
} | |
}) | |
class App extends Component { | |
render() { | |
const { counter, dispatch } = this.props; | |
return ( | |
<div> | |
<span>{counter}</span> | |
<br /> | |
<button onClick={() => dispatch(incrementAction())}>Increment</button> | |
<button onClick={() => dispatch(decrementAction())}>Decrement</button> | |
</div> | |
); | |
} | |
} | |
let rootElement = document.createElement('div'); | |
document.body.appendChild(rootElement); | |
render ( | |
<Provider | |
store={store}> | |
<App /> | |
</Provider>, rootElement | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment