SSX React provides a React Provider and Hook for using SSX in React applications. It is meant to be used in conjunction with wagmi.sh and is compatible with Next.js.
ssx-react provides two key react components: a React Provider and a ReactHook. The Provider is used to wrap components where SSX would be needed, and the Hook is used to access the SSX instance.
Provider
Wagmi
The Provider is used to wrap the application. It takes a single prop, ssxConfig, which is the configuration object for the SSX instance. The Provider will create the SSX instance and make it available to the application via the Hook. An example is provided below:
import { createClient, WagmiConfig } from'wagmi';import { SSXProvider } from'@spruceid/ssx-react';constssxConfig= { siweConfig: { statement:"Sign into my example dapp.", }, providers: { server: { host:"https://api.example.com/" }, },};constwagmiClient=createClient({// your wagmi client configuration// configuration guide at https://wagmi.sh/docs/client});functionApp({ Component, pageProps }:AppProps) {return ( <WagmiConfigclient={wagmiClient}> <SSXProviderssxConfig={ssxConfig}> {/* enables useSSX hook in children */} <Component {...pageProps} /> </SSXProvider> </WagmiConfig> );}
Other Providers
If you're a web3 provider other than wagmi.sh, below is an example of how this can be done. Here we use Blocknative's web3-onboard provider.
import'../styles/globals.css'import { Web3OnboardProvider, init, useConnectWallet } from'@web3-onboard/react'import injectedModule from'@web3-onboard/injected-wallets'import { SSXProvider } from'@spruceid/ssx-react';constweb3Onboard=init({// your web3Onboard configuration// configuration guide at https://onboard.blocknative.com/docs/modules/core#initialization});/** * We create a component that wraps the SSX Provider. It accesses* a web3 provider hook and passes it to the SSX Provider, before* rendering any child components.**/functionSSXWithWeb3Provider({ children }) {// we get the provider from @web3-onboard's react hookconst [{ wallet }] =useConnectWallet();// we build the web3Provider object and pass it to the SSX Providerconstweb3Provider= { provider:wallet?.provider, providerLoaded:!!wallet?.provider, };constssxConfig= { siweConfig: { statement:"Login to my app!", }, };return ( <SSXProviderssxConfig={ssxConfig} web3Provider={web3Provider}> {children} </SSXProvider> );}functionMyApp({ Component, pageProps }) {return ( <Web3OnboardProviderweb3Onboard={web3Onboard}> <SSXWithWeb3Provider> <Component {...pageProps} /> </SSXWithWeb3Provider> </Web3OnboardProvider> )}exportdefault MyApp
Hook
The Hook is used to access the SSX instance. It returns an object with the following properties:
ssx: the SSX instance
ssxLoaded: a boolean indicating whether the SSX is loaded and ready to use