Last active
January 30, 2018 21:34
-
-
Save keller/b3d48ebaaa91368b9c477e5d2bc4382e to your computer and use it in GitHub Desktop.
Simple react-redux implemententation using all "render props" and react-broadcast@next
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, { Component } from "react"; | |
import { createContext } from "react-broadcast"; | |
const StoreContext = createContext(); | |
class Connected extends Component { | |
state = this.props.store.getState(); | |
componentDidMount() { | |
this.props.store.subscribe(() => { | |
this.setState(this.props.store.getState()); | |
}); | |
} | |
render() { | |
return this.props.children({ | |
...this.props.mapStateToProps(this.state), | |
...this.props.mapDispatchToProps(this.props.store.dispatch) | |
}); | |
} | |
} | |
export function Connect(props) { | |
return ( | |
<StoreContext.Consumer> | |
{store => <Connected store={store} {...props} />} | |
</StoreContext.Consumer> | |
); | |
} | |
export function Provider(props) { | |
return ( | |
<StoreContext.Provider value={props.store}> | |
{props.children} | |
</StoreContext.Provider> | |
); | |
} |
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 { render } from "react-dom"; | |
import { Connect, Provider } from "./react-redux"; | |
import { store } from "./store"; | |
render( | |
<Provider store={store}> | |
<App /> | |
</Provider>, | |
document.getElementById("root") | |
); | |
function App() { | |
return ( | |
<Connect | |
mapStateToProps={mapStateToProps} | |
mapDispatchToProps={mapDispatchToProps} | |
> | |
{props => ( | |
<div> | |
... | |
</div> | |
)} | |
</Connect> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment