Build Yourself a Redux (window.state)
This sample code goes along with a Zapier engineering blog post that walks you through building a mini version of redux and react-redux.
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/react-with-addons.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/react-dom.js"></script>
<div id="root"></div>
CSS
body {
font-family: sans-serif;
font-size: 14px;
}
.editor-button {
border: gray solid 1px;
background-color: white;
min-width: 100px;
font-size: 14px;
border-radius: 3px;
}
.note-list {
list-style: none;
padding: 0px;
}
.note-list-item {
font-size: 14px;
margin-bottom: 5px;
}
.editor-content {
font-size: 14px;
margin: 10px 0px 14px;
vertical-align: bottom;
width: 90vw;
height: 50vh;
}
Babel + JSX
const initialState = {
nextNoteId: 1,
notes: {}
};
window.state = initialState;
const onAddNote = () => {
const id = window.state.nextNoteId;
window.state.notes[id] = {
id,
content: ''
};
window.state.nextNoteId++;
renderApp();
};
const NoteApp = ({notes}) => (
<div>
<ul className="note-list">
{
Object.keys(notes).map(id => (
// Obviously we should render something more interesting than the id.
<li className="note-list-item" key={id}>{id}</li>
))
}
</ul>
<button className="editor-button" onClick={onAddNote}>New Note</button>
</div>
);
const renderApp = () => {
ReactDOM.render(
<NoteApp notes={window.state.notes}/>,
document.getElementById('root')
);
};
renderApp();