Skip to content

useConfig

Hook for getting Config from nearest StarkwebProvider.

Import

import { useConfig } from "starkweb/react"

Usage

index.tsx
import { useConfig } from "starkweb/react"
 
function App() {
  const config = useConfig()
}

Return Type

import { type UseConfigReturnType } from "starkweb/react"

chains

readonly [Chain, ...Chain[]]

chains passed to createConfig.

connectors

readonly Connectors[]

Connectors set up from passing connectors and multiInjectedProviderDiscovery to createConfig.

state

readonly State

The Config object's internal state. See State for more info.

storage

readonly Storage | null

storage passed to createConfig.

getClient

(parameters?: { chainId?: chainId | undefined }): Client<transports[chains[number]["id"]], Extract<chains[number], { chain_id: chainId }>>

Creates new Client object.

index.ts
import { config } from "./config"
 
const client = config.getClient({ chainId: 1 })

setState

(value: State | ((state: State) => State)) => void

Updates the Config object's internal state. See State for more info.

index.ts
import { mainnet } from "starkweb/react/chains"
import { config } from "./config"
 
config.setState((x) => ({
  ...x,
  chainId: x.current ? x.chainId : mainnet.chain_id,
}))

subscribe

(selector: (state: State>) => state, listener: (selectedState: state, previousSelectedState: state) => void, options?: { emitImmediately?: boolean | undefined; equalityFn?: ((a: state, b: state) => boolean) | undefined } | undefined) => (() => void)

Listens for state changes matching the selector function. Returns a function that can be called to unsubscribe the listener.

index.ts
import { config } from "./config"
 
const unsubscribe = config.subscribe(
  (state) => state.chainId,
  (chainId) => console.log(`Chain ID changed to ${chainId}`),
)
unsubscribe()