Build Yourself a Redux (feed actions array into reducer)
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 CREATE_NOTE = 'CREATE_NOTE';
const UPDATE_NOTE = 'UPDATE_NOTE';
const initialState = {
nextNoteId: 1,
notes: {}
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case CREATE_NOTE: {
const id = state.nextNoteId;
const newNote = {
id,
content: ''
};
return {
...state,
nextNoteId: id + 1,
notes: {
...state.notes,
[id]: newNote
}
};
}
case UPDATE_NOTE: {
const {id, content} = action;
const editedNote = {
...state.notes[id],
content
};
return {
...state,
notes: {
...state.notes,
[id]: editedNote
}
};
}
default:
return state;
}
};
const actions = [
{type: CREATE_NOTE},
{type: UPDATE_NOTE, id: 1, content: 'Hello, world !'},
{type: UPDATE_NOTE, id: 2, content: 'Me'}
];
const state = actions.reduce(reducer, undefined);
ReactDOM.render(
<pre>{JSON.stringify(state, null, 2)}</pre>,
document.getElementById('root')
);