Skip to content

Instantly share code, notes, and snippets.

@keller
Last active January 30, 2018 21:34
Show Gist options
  • Save keller/b3d48ebaaa91368b9c477e5d2bc4382e to your computer and use it in GitHub Desktop.
Save keller/b3d48ebaaa91368b9c477e5d2bc4382e to your computer and use it in GitHub Desktop.
Simple react-redux implemententation using all "render props" and react-broadcast@next
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>
);
}
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