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

Improve-Type-Safety-and-State-Access-in-useStateWithDeps-Hook #3027

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
7 changes: 6 additions & 1 deletion src/mutation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ const mutation = (<Data, Error>() =>
// Ditch all mutation results that happened earlier than this timestamp.
const ditchMutationsUntilRef = useRef(0)

const [stateRef, stateDependencies, setState] = useStateWithDeps({
const [stateRef, stateDependencies, setState] = useStateWithDeps<{
data: Data | undefined
error: Error | undefined
isMutating: boolean
}>({
data: UNDEFINED,
error: UNDEFINED,
isMutating: false
})

const currentState = stateRef.current

const trigger = useCallback(
Expand Down
35 changes: 20 additions & 15 deletions src/mutation/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ export const startTransition: (scope: TransitionFunction) => void =

/**
* An implementation of state with dependency-tracking.
* @param initialState - The initial state object.
*/
export const useStateWithDeps = <S = any>(
state: any
export const useStateWithDeps = <S = Record<string, any>>(
initialState: S
): [
MutableRefObject<any>,
MutableRefObject<S>,
Record<keyof S, boolean>,
(payload: Partial<S>) => void
] => {
const [, rerender] = useState<Record<string, unknown>>({})
const unmountedRef = useRef(false)
const stateRef = useRef(state)
const stateRef = useRef<S>(initialState)

// If a state property (data, error, or isValidating) is accessed by the render
// function, we mark the property as a dependency so if it is updated again
Expand All @@ -31,9 +32,10 @@ export const useStateWithDeps = <S = any>(
data: false,
error: false,
isValidating: false
} as any)
} as Record<keyof S, boolean>)

/**
* Updates state and triggers re-render if necessary.
* @param payload To change stateRef, pass the values explicitly to setState:
* @example
* ```js
Expand All @@ -54,18 +56,21 @@ export const useStateWithDeps = <S = any>(
let shouldRerender = false

const currentState = stateRef.current
for (const _ in payload) {
const k = _ as keyof S
for (const key in payload) {
if (Object.prototype.hasOwnProperty.call(payload, key)) {
const k = key as keyof S

// If the property has changed, update the state and mark rerender as
// needed.
if (currentState[k] !== payload[k]) {
currentState[k] = payload[k]
// If the property has changed, update the state and mark rerender as
// needed.
if (currentState[k] !== payload[k]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
currentState[k] = payload[k]!

// If the property is accessed by the component, a rerender should be
// triggered.
if (stateDependenciesRef.current[k]) {
shouldRerender = true
// If the property is accessed by the component, a rerender should be
// triggered.
if (stateDependenciesRef.current[k]) {
shouldRerender = true
}
}
}
}
Expand Down
Loading