Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Last active October 25, 2017 18:34
Show Gist options
  • Save dead-claudia/a728fc324ecc4643e681ee9c1e6c9131 to your computer and use it in GitHub Desktop.
Save dead-claudia/a728fc324ecc4643e681ee9c1e6c9131 to your computer and use it in GitHub Desktop.
Redux Zero in pure ES6 without React context or higher-order component support (with Mithril equivalent)
import m from "mithril"
import { Connect } from "./redux-zero-mithril"
import { increment, decrement } from "./actions"
const mapToProps = ({ count }) => ({ count })
export default function Counter() {
return {view({attrs}) {
return m(Connect, {to: attrs.store, with: mapToProps, view({count}) {
return m("div", [
m("h1", count),
m("div", [
m("button", {onclick: increment}, "increment"),
m("button", {onclick: decrement}, "decrement"),
]),
]),
}})
}}
}
import React from "react"
import { Connect } from "./redux-zero"
import { increment, decrement } from "./actions"
const mapToProps = ({ count }) => ({ count })
export default function Counter(props) {
return (
<Connect to={props.store} with={mapToProps}>{({count}) => (
<div>
<h1>{count}</h1>
<div>
<button onClick={increment}>increment</button>
<button onClick={decrement}>decrement</button>
</div>
</div>
)}</Connect>
)
}
import m from "mithril"
export default function createStore(state = {}) {
const listeners = []
return {
set(update) {
Object.assign(state, typeof update === "function"
? update(state)
: update)
listeners.forEach(f => f(state))
},
listen(f) {
listeners.push(f)
return () => {
listeners.splice(listeners.indexOf(f), 1)
}
},
get() {
return state
}
}
}
function shallowEqual(a, b) {
return Object.keys(a).every(i => a[i] === b[i]) &&
Object.keys(b).every(i => hasOwnProperty.call(a, i))
}
export function Connect({attrs}) {
let state = attrs.with(attrs.to.get())
const listener = () => {
const mapped = attrs.with(attrs.to.get())
if (!shallowEqual(mapped, state)) {
state = mapped
m.redraw()
}
}
return {
onremove: attrs.to.listen(listener),
view: () => attrs.view(state),
}
}
import * as React from "react"
export default function createStore(state = {}) {
const listeners = []
return {
set(update) {
Object.assign(state, typeof update === "function"
? update(state)
: update)
listeners.forEach(f => f(state))
},
listen(f) {
listeners.push(f)
return () => {
listeners.splice(listeners.indexOf(f), 1)
}
},
get() {
return state
}
}
}
function shallowEqual(a, b) {
return Object.keys(a).every(i => a[i] === b[i]) &&
Object.keys(b).every(i => hasOwnProperty.call(a, i))
}
export class Connect extends React.Component {
constructor(props) {
super(props)
this.listener = () => {
const mapped = props.with(props.to.get())
if (!shallowEqual(mapped, this.state)) {
this.setState(mapped)
}
}
this.unlisten = props.to.listen(this.listener)
this.state = props.with(props.to.get())
}
componentDidUnmount() {
this.unlisten()
}
render() {
return this.props.children(this.state)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment