-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Context/Provider/Subscription to Typescript (#1742)
- Loading branch information
Showing
8 changed files
with
99 additions
and
77 deletions.
There are no files selected for viewing
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 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,17 @@ | ||
import React from 'react' | ||
import { Action, AnyAction, Store } from 'redux' | ||
import type { FixTypeLater } from '../types' | ||
import type Subscription from '../utils/Subscription' | ||
|
||
export interface ReactReduxContextValue<SS = FixTypeLater, A extends Action = AnyAction> { | ||
store: Store<SS, A> | ||
subscription: Subscription | ||
} | ||
|
||
export const ReactReduxContext = /*#__PURE__*/ React.createContext<ReactReduxContextValue | null>(null) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
ReactReduxContext.displayName = 'ReactRedux' | ||
} | ||
|
||
export default ReactReduxContext |
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,52 @@ | ||
import React, { Context, ReactNode, useMemo } from 'react' | ||
import { ReactReduxContext, ReactReduxContextValue } from './Context' | ||
import Subscription from '../utils/Subscription' | ||
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect' | ||
import type { FixTypeLater } from '../types' | ||
import { Action, AnyAction, Store } from 'redux' | ||
|
||
interface ProviderProps<A extends Action = AnyAction> { | ||
/** | ||
* The single Redux store in your application. | ||
*/ | ||
store: Store<FixTypeLater, A> | ||
/** | ||
* Optional context to be used internally in react-redux. Use React.createContext() to create a context to be used. | ||
* If this is used, generate own connect HOC by using connectAdvanced, supplying the same context provided to the | ||
* Provider. Initial value doesn't matter, as it is overwritten with the internal state of Provider. | ||
*/ | ||
context?: Context<ReactReduxContextValue> | ||
children: ReactNode | ||
} | ||
|
||
function Provider({ store, context, children }: ProviderProps) { | ||
const contextValue = useMemo(() => { | ||
const subscription = new Subscription(store) | ||
subscription.onStateChange = subscription.notifyNestedSubs | ||
return { | ||
store, | ||
subscription, | ||
} | ||
}, [store]) | ||
|
||
const previousState = useMemo(() => store.getState(), [store]) | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const { subscription } = contextValue | ||
subscription.trySubscribe() | ||
|
||
if (previousState !== store.getState()) { | ||
subscription.notifyNestedSubs() | ||
} | ||
return () => { | ||
subscription.tryUnsubscribe() | ||
subscription.onStateChange = undefined | ||
} | ||
}, [contextValue, previousState]) | ||
|
||
const Context = context || ReactReduxContext | ||
|
||
return <Context.Provider value={contextValue}>{children}</Context.Provider> | ||
} | ||
|
||
export default Provider |
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 @@ | ||
export type FixTypeLater = any |
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