> For the complete documentation index, see [llms.txt](https://docs.megaeth.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.megaeth.com/moss-docs/react-sdk/provider-setup.md).

# Provider Setup

The React SDK exposes a single provider component that initialises MOSS and injects a React Query client for wallet hooks. **Initialisation runs once per page load** — a module-level guard prevents re-initialisation on re-renders or remount. The `QueryClient` is also a module-level singleton, shared across all `MegaProvider` instances on the page.

## Basic Usage

```tsx
import { MegaProvider } from '@megaeth-labs/wallet-sdk-react';

export function AppShell() {
  return (
    <MegaProvider config={{ network: 'mainnet', logging: 'error' }}>
      <App />
    </MegaProvider>
  );
}
```

## Observed Provider Behavior

* Stores `initialised` and `status` in React state
* Registers `mega.events.onStatusChange(setStatus)`
* Calls `mega.initialise(config)` once inside `useEffect`
* Wraps children in a `QueryClientProvider`

## Status Shape in App Code

```tsx
const { status, address, network, initialised } = useStatus();

if (!initialised) return <p>Loading wallet…</p>;
if (status === 'connected') return <p>Connected account: {address} on {network}</p>;
return <p>No connected account</p>;
```

Use this status model to drive account access UI and loading states.

## Placement Guidance

* Put `MegaProvider` high enough that all wallet-aware routes can access hooks.
* Avoid mounting and unmounting it repeatedly during normal navigation.
* Because initialisation runs once per page load, **changing `config` after the first render does not re-initialise** the wallet — set the right network/logging config from the start.
* If your app already owns a global React Query client strategy, test provider nesting carefully — the package's internal `QueryClient` is a module-level singleton.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.megaeth.com/moss-docs/react-sdk/provider-setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
