Created
December 22, 2016 12:54
-
-
Save rscarvalho/0f7f23a69df7cfee64aedbc817f5ecd9 to your computer and use it in GitHub Desktop.
This file contains 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 React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import Redux from 'redux'; | |
import { createStore } from 'redux'; | |
import { connect, Provider } from 'react-redux'; | |
class MyComponent extends React.Component { | |
render() { | |
const { active, toggle } = this.props; | |
return ( | |
<div> | |
<h1>Hello World</h1> | |
<button onClick={toggle}>click me</button> | |
<div>{active}</div> | |
</div> | |
) | |
} | |
} | |
const someReducer = (state = { active: false }, action) => { | |
switch (action.type) { | |
case 'TOGGLE': | |
return { | |
active: !state.active | |
} | |
default: | |
return state | |
} | |
} | |
const store = createStore(someReducer); | |
const mapStateToProps = function(store) { | |
return { | |
active: store.active | |
}; | |
} | |
const mapActionsToProps = { | |
toggle: () => ({type: 'TOGGLE'}) | |
}; | |
connect(mapStateToProps, mapActionsToProps)(MyComponent); | |
ReactDOM.render( | |
<Provider store={store}> | |
<MyComponent /> | |
</Provider>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment