👋
|
const reRender = () => set({}); |
React hook FAQ советует использовать следующий подход:
const [, reRender] = useReducer(x => x + 1, 0);
|
const reRender = () => set({}); |
|
useEffect(() => { |
|
bState.joinReRender(reRender); |
|
return () => { |
|
bState.cancelReRender(reRender); |
|
}; |
|
}, [bState]); |
Функция
reRender
создаётся новая на каждый рендер, а хук
useEffect
отрабатывает только на изменение
bState
, в результате чего в
cancelReRender
попадает
другая функция
reRender
, чем была добавлена в
joinReRender
, то есть старые функции не удаляются, что вызывает утечку памяти. Если заюзать подход из п.1, проблема должна уйти, т.к. функция будет стабильная.
|
function setState(newState) { |
|
bState.set(newState); |
|
} |
Функция
setState
создаётся новая на каждый рендер, это поведение не аналогично хуку
useState
, где функция стабильная. Это может вызвать проблемы с рендерами, если пользователь не будет учитывать этот момент.
|
return Object.freeze(Object.create(prototype)); |
Создание объекта через
Object.freeze(Object.create(...))
выглядит как ненужное переусложнение на мой взгляд. Это выглядит как попытка защитить объект от изменений вручную, но такая «защита» без проблем обходится
PS
Я не пробовал код в работе, это просто визуальное ревью.
Замечу, что написание стейт менеджера это очень сложная задача, гораздо сложнее, чем кажется на первый взгляд.