Provider Setup
Configure NeroMpcAuthProvider to integrate the Nero MPC SDK with your React component tree
Provider Setup
NeroMpcAuthProvider is the root React context provider that integrates the core NeroMpcSDK with your component tree. It manages SDK instantiation, synchronizes SDK events into React state, and exposes authentication and wallet state through context.
Basic Setup
import { NeroMpcAuthProvider } from "@nerochain/mpc-sdk/react";
function App() {
return (
<NeroMpcAuthProvider
config={{
backendUrl: "https://your-backend.example.com",
chainId: 689,
protocol: "dkls",
}}
>
<YourApp />
</NeroMpcAuthProvider>
);
}Props Interface
| Prop | Type | Default | Purpose |
|---|---|---|---|
children | ReactNode | Required | React subtree to render |
config | SDKConfig | Required | SDK configuration passed to constructor |
autoConnect | boolean | true | Automatically calls sdk.initialize() on mount |
uiConfig | UIConfig | undefined | Visual customization settings |
themeMode | ThemeMode | "auto" | Theme preference: "light", "dark", or "auto" |
Initialization Lifecycle
When the component mounts, a useEffect creates the SDK instance. The dependency array includes config.backendUrl, config.chainId, and autoConnect, meaning SDK recreation occurs if these values change at runtime.
The autoConnect flag controls automatic session restoration:
true(default):sdk.initialize()is called to attempt loading persisted session datafalse: The SDK instance is created but not initialized, requiring manual initialization by consumers
Event Synchronization
A second useEffect registers listeners on the SDK's event bus after the instance becomes available. Event listeners pull the latest NeroSDKState snapshot into React state, triggering re-renders when authentication or connection status changes.
All listeners are properly cleaned up in the effect's cleanup function.
Context Value Shape
The NeroMpcAuthContextValue exposed to descendants:
| Field | Type | Description |
|---|---|---|
sdk | NeroMpcSDK | null | Live SDK instance; null briefly before first effect runs |
state | NeroSDKState | Current SDK state snapshot |
isLoading | boolean | true while initialization is in progress |
error | Error | null | Set when initialization throws; reset on retry |
NeroSDKState Default Values
| Field | Initial Value | Description |
|---|---|---|
isAuthenticated | false | Valid user session exists |
isInitialized | false | Initialization has completed |
hasWallet | false | Key share available for user |
user | null | Authenticated user object or null |
walletInfo | null | Current wallet information or null |
chainId | 689 | Active chain identifier |
isConnected | false | EIP-1193 provider connection status |
Accessing the SDK Instance
For advanced use cases, access the underlying SDK through the context hook:
import { useNeroMpcAuthContext } from "@nerochain/mpc-sdk/react";
function Advanced() {
const { sdk, state, isLoading, error } = useNeroMpcAuthContext();
if (isLoading) return <div>Initializing...</div>;
if (error) return <div>Error: {error.message}</div>;
const provider = sdk?.getProvider();
}The useNeroMpcAuthContext() hook throws an error if called outside a NeroMpcAuthProvider:
useNeroMpcAuthContext must be used within a NeroMpcAuthProviderComponent Tree Structure
The provider renders ThemeProvider as the outermost wrapper, ensuring all modal and UI components have access to theme context. Inside that sits NeroMpcAuthContext.Provider, allowing hooks to access both theme and authentication contexts simultaneously.
<NeroMpcAuthProvider config={config} uiConfig={uiConfig} themeMode="dark">
{/* ThemeProvider wraps everything */}
{/* NeroMpcAuthContext.Provider is inside ThemeProvider */}
<YourApp />
</NeroMpcAuthProvider>Key Behavioral Points
- Config reactivity: Changing
backendUrl,chainId, orautoConnectat runtime destroys the old SDK and creates a new one - Error isolation: SDK instances are available in context even when initialization fails; the
errorfield captures the failure - Memoization: Context value is wrapped in
useMemoand only recomputes whensdk,state,isLoading, orerrorchanges