Skip to content

Instantly share code, notes, and snippets.

Avatar

David Gilbertson davidgilbertson

  • Sydney, Australia
View GitHub Profile
View storage.ts
const hasLocalStorage = typeof window !== 'undefined' && !!window.localStorage;
export const set = (key: string, data: any) => {
if (!hasLocalStorage) return undefined;
try {
const string = typeof data === 'string' ? data : JSON.stringify(data);
return localStorage.setItem(key, string);
} catch (err) {
View exampleDiff.md
+import { store } from 'react-recollect'; // New line

export function toggleTodo(index) {
+  store.todos[index].completed = !store.todos[index].completed; // New line
  
-  return { type: TOGGLE_TODO, index }
}
View App.jsx
import React from 'react';
import { collect } from 'react-recollect';
const App = ({ store }) => {
const { thatThing } = store.site.page.meta.data;
const { anotherThing } = store.ui;
const { aThirdThing } = store.maps.schemas.plans.operations;
return /* ... */
};
View loadTodosRecollect.js
import { store } from 'react-recollect';
const loadTodos = async () => {
const userId = store.user.id;
store.todos = await fetch(`/api/todos/${userId}`).then((resp) => resp.json());
};
View loadTodosRedux.js
const loadTodos = () => async (dispatch, getState) => {
const userId = getState().user.id;
const todos = await fetch(`/api/todos/${userId}`).then((resp) => resp.json());
dispatch({
type: 'LOADED_TODOS',
data: todos,
});
};
View compareStores.js
console.assert(
JSON.stringify(recollectStore.todos) === JSON.stringify(reduxStore.getState().todos),
'The two stores should match'
);
View toggleTodo.js
import { store } from 'react-recollect'; // New line
export function toggleTodo(index) {
store.todos[index].completed = !store.todos[index].completed; // New line
return { type: TOGGLE_TODO, index }
}
View reducer.js
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
View setVisibilityFilter.js
import { store } from 'react-recollect'; // New line
export function setVisibilityFilter(filter) {
store.visibilityFilter = filter; // New line
return { type: SET_VISIBILITY_FILTER, filter }; // We'll delete this later
}
View setVisibilityFilter.js
export function setVisibilityFilter(filter) {
return { type: SET_VISIBILITY_FILTER, filter };
}
You can’t perform that action at this time.