Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useImmediateState for React Native #358

Merged
merged 2 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/hooks/useImmediateState.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
import { useRef, useState, useCallback, useLayoutEffect } from 'react';
import { isSSR } from '../utils';
import {
useRef,
useState,
useCallback,
useEffect,
useLayoutEffect,
} from 'react';

type SetStateAction<S> = S | ((prevState: S) => S);
type SetState<S> = (action: SetStateAction<S>) => void;

// See https://github.com/reduxjs/react-redux/blob/316467a/src/hooks/useSelector.js#L6-L15
const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect;

/**
* This is a drop-in replacement for useState, limited to object-based state.
* During initial mount it will mutably update the state, instead of scheduling
* a React update using setState
*/
export const useImmediateState = <S extends {}>(init: S): [S, SetState<S>] => {
const isMounted = useRef(false);
const initialState = useRef<S>({ ...init });
const [state, setState] = useState<S>(initialState.current);
const [state, setState] = useState<S>(init);

// This wraps setState and updates the state mutably on initial mount
const updateState: SetState<S> = useCallback(
(action: SetStateAction<S>): void => {
if (!isMounted.current) {
const newValue =
const newState =
typeof action === 'function'
? (action as (arg: S) => S)(initialState.current)
? (action as (arg: S) => S)(state)
: action;
Object.assign(initialState.current, newValue);
Object.assign(state, newState);
} else {
setState(action);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);

!isSSR && // eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
useIsomorphicLayoutEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);

return [state, updateState];
};
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './error';
export * from './keyForQuery';
export * from './request';
export * from './ssr';
export * from './typenames';
export * from './toSuspenseSource';

Expand Down
2 changes: 0 additions & 2 deletions src/utils/ssr.ts

This file was deleted.