Last active
November 11, 2015 16:15
-
-
Save henriklundgren/c152827339684b6c2169 to your computer and use it in GitHub Desktop.
React with Baobab
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 ReactDOM from 'react-dom'; | |
import { | |
branch, | |
root, | |
} from 'baobab-react/higher-order'; | |
import Baobab from 'baobab'; | |
const state = new Baobab({ | |
title: 'Foobar', | |
colors: ['yellow', 'blue', 'red', 'brown'] | |
}); | |
const addColor = function addColor(state, color) { | |
return state.push('colors', color); | |
}; | |
const Title = branch(function Title(props) { | |
function componentDidMount() { | |
props.actions.addColor('green'); | |
} | |
function render() { | |
return ( | |
<div> | |
<h1>{ this.props.title }</h1> | |
<ul>{ this.props.colors.map(renderList) }</ul> | |
</div> | |
); | |
} | |
function renderList(value, key) { | |
return <li key={key}>{value}</li>; | |
} | |
return { props, render, componentDidMount }; | |
}, { | |
cursors: { | |
title: ['title'], | |
colors: ['colors'] | |
}, | |
actions: { addColor } | |
}); | |
const Application = root(function Application(props) { | |
function render() { | |
return <Title />; | |
} | |
return { props, render }; | |
}, state); | |
ReactDOM.render(<Application />, document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 36
Each cursor needs to be a list.