Skip to content

Instantly share code, notes, and snippets.

@fleepgeek
Last active September 7, 2019 18:56
Show Gist options
  • Save fleepgeek/34d8cc46167213ad728aa0786cf14034 to your computer and use it in GitHub Desktop.
Save fleepgeek/34d8cc46167213ad728aa0786cf14034 to your computer and use it in GitHub Desktop.
A simple gist that uses redux without react.
// Quotes App
const { createStore } = require("redux");
// Reducer - updates the store
const initialState = [
{
author: "Walt Disney",
quote: "The way to get started is to quit talking and begin doing."
}
];
const reducer = (state = initialState, action) => {
switch (action.type) {
case "ADD_QUOTE":
return [...state, action.payload];
default:
return state;
}
};
// Store - the single source of truth
const store = createStore(reducer);
// console.log(store.getState());
// Subscription - listens for actions and store updates
store.subscribe(() => {
console.log(store.getState());
});
// Actions - information that is sent to the reducer
const addQuote = (author, quote) => ({
type: "ADD_QUOTE",
payload: { author, quote }
});
store.dispatch(
addQuote(
"John Lennon",
"Life is what happens when you're busy making other plans."
)
);
// Dispatches after 3 seconds.
// Used to show that the subscription listens for store updates.
// The subscription executes a console.log() when this action is dispatched
setTimeout(() => {
store.dispatch(
addQuote("Anne Frank", "Whoever is happy will make others happy too.")
);
}, 3000);
// console.log(store.getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment