Created
February 13, 2018 04:40
-
-
Save quinn/66317aeee5d278d3d013dc8f38999eb2 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' | |
var store = { | |
_finishedUpdating () { | |
this.listeners.forEach(listener => { | |
var value = findInTree(state, listener.property) | |
listener.callback(value) | |
}) | |
} | |
} | |
// support various operations | |
store.set('some.path', { new: 'value' }) | |
store.incr('numeric.value') | |
store.push('array.thing', newVal) | |
const makeListener = (Component, propertyMap) => | |
class extends React.Component { | |
componentWillMount () { | |
this.cancels = Object.keys(propertyMap).map(property => | |
createListener(property, value => | |
this.setState(propertyMap[property], value) | |
) | |
) | |
} | |
render () { | |
return <Component {...Object.keys(propertyMap).reduce((property, acc) => { | |
var propName = propertyMap[property] | |
, value = this.state[propName] | |
acc[propName] = value | |
acc | |
}, {})} /> | |
} | |
} | |
// Example | |
const FloopyComponent = ({ prop1, prop2 }) => | |
<div>hi {prop1} hi {prop2}</div> | |
makeListener(FloopyComponent, { | |
'path.to.prop1': 'prop1', | |
'path.to.prop2': 'prop2', | |
}) | |
let state = { | |
path: { | |
to: { | |
prop1: 'hello', | |
prop2: 'world', | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment