-
-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix broken ssr * update tests * update tests * remove ssr abstraction * don't call useLayoutEffect in SSR * update SSR check * ssr....
- Loading branch information
1 parent
b4f62a9
commit 40e371b
Showing
5 changed files
with
72 additions
and
68 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { useImmediateState } from './useImmediateState'; | ||
|
||
const setStateMock = jest.fn(); | ||
jest.spyOn(React, 'useState').mockImplementation(arg => [arg, setStateMock]); | ||
|
||
const initialState = { someObject: 1234 }; | ||
const updateState = { someObject: 5678 }; | ||
let state; | ||
let setState; | ||
|
||
const Fixture = ({ update }: { update?: boolean }) => { | ||
const [a, set] = useImmediateState<object>(initialState); | ||
|
||
if (update) { | ||
set(updateState); | ||
} | ||
|
||
state = a; | ||
setState = set; | ||
|
||
return null; | ||
}; | ||
|
||
beforeEach(jest.clearAllMocks); | ||
|
||
describe('on initial mount', () => { | ||
it('sets initial state', () => { | ||
renderer.create(<Fixture />); | ||
expect(state).toEqual(initialState); | ||
}); | ||
|
||
it('only mutates on setState call', () => { | ||
renderer.create(<Fixture update={true} />); | ||
expect(setStateMock).toBeCalledTimes(0); | ||
expect(state).toEqual(updateState); | ||
}); | ||
}); | ||
|
||
describe('on later mounts', () => { | ||
it('sets state via setState', () => { | ||
renderer.create(<Fixture />); | ||
setState(updateState); | ||
expect(setStateMock).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
import { useRef, useEffect, useState, useCallback } from 'react'; | ||
import { useRef, useState, useCallback, useLayoutEffect } from 'react'; | ||
import { isSSR } from '../utils'; | ||
|
||
type SetStateAction<S> = S | ((prevState: S) => S); | ||
type SetState<S> = (action: SetStateAction<S>) => void; | ||
|
||
/** 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 */ | ||
/** | ||
* 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); | ||
|
||
// This wraps setState and updates the state mutably on initial mount | ||
// It also prevents setting the state when the component is unmounted | ||
const updateState: SetState<S> = useCallback((action: SetStateAction<S>) => { | ||
if (isMounted.current) { | ||
setState(action); | ||
} else if (typeof action === 'function') { | ||
const update = (action as any)(initialState.current); | ||
Object.assign(initialState.current, update); | ||
} else { | ||
// There are scenario's where we are in-between mounts. | ||
// The reason we need both is because we COULD still be mounting | ||
// and we could be in the process of being mounted. | ||
// Scenario 1 needs the initialState.current mutation. | ||
// Scenario 2 needs the setState. | ||
// See https://github.com/FormidableLabs/urql/issues/287 | ||
setState(() => Object.assign(initialState.current, action as any)); | ||
if (!isMounted.current) { | ||
const newValue = | ||
typeof action === 'function' | ||
? (action as (arg: S) => S)(initialState.current) | ||
: action; | ||
return Object.assign(initialState.current, newValue); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
setState(action); | ||
}, []); | ||
|
||
!isSSR && // eslint-disable-next-line react-hooks/rules-of-hooks | ||
useLayoutEffect(() => { | ||
isMounted.current = true; | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
|
||
return [state, updateState]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const isSSR = | ||
typeof window === 'undefined' || !('HTMLElement' in window); |