Skip to content

Commit

Permalink
pass hooks through context
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Apr 28, 2022
1 parent 0b890ba commit b2a4674
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Web3ContextType<T extends BaseProvider = Web3Provider> = {
provider: T | undefined
ENSNames: ReturnType<Web3ReactPriorityHooks['useSelectedENSNames']>
ENSName: ReturnType<Web3ReactPriorityHooks['useSelectedENSName']>
hooks: ReturnType<typeof getPriorityConnector>
}

const Web3Context = createContext<Web3ContextType | undefined>(undefined)
Expand Down Expand Up @@ -50,6 +51,7 @@ export function Web3ReactProvider({
network,
lookupENS = true,
}: Web3ReactProviderProps) {
const hooks = getPriorityConnector(...connectors)
const {
usePriorityConnector,
useSelectedChainId,
Expand All @@ -61,7 +63,7 @@ export function Web3ReactProvider({
useSelectedProvider,
useSelectedENSNames,
useSelectedENSName,
} = getPriorityConnector(...connectors)
} = hooks

const priorityConnector = usePriorityConnector()
const connector = connectorOverride ?? priorityConnector
Expand Down Expand Up @@ -94,15 +96,16 @@ export function Web3ReactProvider({
provider,
ENSNames,
ENSName,
hooks,
}}
>
{children}
</Web3Context.Provider>
)
}

export function useWeb3React<T extends BaseProvider = Web3Provider>() {
const web3 = useContext(Web3Context as Context<Web3ContextType<T> | undefined>)
if (!web3) throw Error('useWeb3React can only be used within the Web3ReactProvider component')
return web3
export function useWeb3React<T extends BaseProvider = Web3Provider>(): Web3ContextType<T> {
const context = useContext(Web3Context as Context<Web3ContextType<T> | undefined>)
if (!context) throw Error('useWeb3React can only be used within the Web3ReactProvider component')
return context
}

0 comments on commit b2a4674

Please sign in to comment.