Skip to content

Instantly share code, notes, and snippets.

@henriklundgren
Last active November 11, 2015 16:15
Show Gist options
  • Save henriklundgren/c152827339684b6c2169 to your computer and use it in GitHub Desktop.
Save henriklundgren/c152827339684b6c2169 to your computer and use it in GitHub Desktop.
React with Baobab
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);
@demux
Copy link

demux commented Nov 10, 2015

line 36
Each cursor needs to be a list.

@henriklundgren
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment