Last active
July 15, 2016 21:38
-
-
Save pixeldrew/b5221a409e5ef743534aa5c658429d25 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 {createStore} from 'redux'; | |
import {Provider} from 'react-redux'; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import {rootReducer, middleware} from 'some-other-module'; | |
const initialData = {'foo': 'Foo', 'bar': 'Bar'}; // perhaps loaded by the server and embedded in the html ie..: const initialData = <? echo $json ?> | |
const Store = createStore(rootReducer, initialData, middleware); | |
(async () => { | |
try { | |
const response = await fetch('some-ajax-foo-bar'); | |
const data = await response.json(); | |
const x = ReactDOM.render( | |
<Provider store={Store}> | |
<RootComponent initialData={data} /> | |
</Provider> , document.getElementById('main')); | |
} catch (e) { | |
// let the user know the load failed... | |
} })(); |
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 {connect} from 'react-redux'; | |
import {populateStore} from 'actions'; // simple action | |
// (state, props) => ({}) | |
// state is the redux store | |
// props is provided to the HigherOrderComponent on line 16 of loadDefaultState.js | |
const mapStateToProps = (state, {initialData}}) => ({ | |
populateStore, | |
initialData, | |
foo: state.foo, | |
bar: state.bar | |
}); | |
export class RootComponent extends React.Component { | |
componentWillMount() { | |
this.props.populateStore(this.props.initialData); | |
} | |
render() { | |
return (<div>I have lots of {foo} and lots of {bar}</div>); | |
} | |
} | |
// connect returns a higher order component that listens for all of the redux store changes | |
// and renders you a component with props that are values from the store. | |
export default connect(mapStateToProps, {populateStore})(RootComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment