This commit is contained in:
2020-05-25 16:07:30 +03:00
parent 0753359f07
commit 13971100cf
55 changed files with 32279 additions and 6964 deletions

View File

@@ -0,0 +1,23 @@
import { useEffect, useRef } from 'react';
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
const useInterval = (callback, delay) => {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
useEffect(() => {
const tick = () => savedCallback.current();
if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
return () => {};
}, [delay]);
};
export default useInterval;

View File

@@ -0,0 +1,16 @@
import { useEffect } from 'react';
const DEFAULT_STATE_KEY = 'state';
export const usePersistedContext = (context, key = DEFAULT_STATE_KEY) => {
const persistedContext = localStorage.getItem(key);
return persistedContext ? JSON.parse(persistedContext) : context;
};
export const usePersistedReducer = ([state, dispatch], key = DEFAULT_STATE_KEY) => {
useEffect(
() => localStorage.setItem(key, JSON.stringify(state)),
[key, state],
);
return [state, dispatch];
};