-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Document React SDK packages #918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
wallet/how-to/connect/set-up-sdk/javascript/react/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| --- | ||
| sidebar_label: React | ||
| sidebar_position: 1 | ||
| --- | ||
|
|
||
| # Use MetaMask SDK with React | ||
|
|
||
| You can import [MetaMask SDK](../../../../../concepts/sdk.md) into your React dapp to enable your users to | ||
| easily connect to the MetaMask browser extension and MetaMask Mobile. | ||
| The SDK for React has the [same prerequisites](../index.md#prerequisites) as for standard JavaScript. | ||
|
|
||
| :::info React UI | ||
| This page provides instructions for using the standard `@metamask/sdk-react` package. | ||
| Alternatively, you can use the [`@metamask/sdk-react-ui`](react-ui.md) package to easily use | ||
| [wagmi](https://wagmi.sh/) hooks and a pre-styled UI button component for connecting to MetaMask. | ||
| ::: | ||
|
|
||
| :::tip Examples | ||
| Refer to the [MetaMask JavaScript SDK examples](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples) | ||
| for advanced use cases. | ||
| ::: | ||
|
|
||
| ## Steps | ||
|
|
||
| ### 1. Install the SDK | ||
|
|
||
| In your project directory, install the SDK using Yarn or npm: | ||
|
|
||
| ```bash | ||
| yarn add @metamask/sdk-react | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ```bash | ||
| npm i @metamask/sdk-react | ||
| ``` | ||
|
|
||
| ### 2. Import the SDK | ||
|
|
||
| In your project script, add the following to import the SDK: | ||
|
|
||
| ```javascript | ||
| import { MetaMaskProvider } from '@metamask/sdk-react'; | ||
| ``` | ||
|
|
||
| ### 3. Wrap your project with MetaMaskProvider | ||
|
|
||
| Wrap your root component in a `MetaMaskProvider`. | ||
| For example: | ||
|
|
||
| ```js | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import App from './App'; | ||
| import { MetaMaskProvider } from '@metamask/sdk-react'; | ||
|
|
||
| const root = ReactDOM.createRoot( | ||
| document.getElementById('root') as HTMLElement | ||
| ); | ||
|
|
||
| root.render( | ||
| <React.StrictMode> | ||
| <MetaMaskProvider debug={false} sdkOptions={{ | ||
| logging:{ | ||
| developerMode: false, | ||
| }, | ||
| communicationServerUrl: process.env.REACT_APP_COMM_SERVER_URL, | ||
| checkInstallationImmediately: false, // This will automatically connect to MetaMask on page load | ||
| dappMetadata: { | ||
| name: "Demo React App", | ||
| url: window.location.host, | ||
| } | ||
| }}> | ||
| <App /> | ||
| </MetaMaskProvider> | ||
| </React.StrictMode> | ||
| ); | ||
| ``` | ||
|
|
||
| When initializing `MetaMaskProvider`, setting `debug` to `true` activates debug mode. | ||
| For the full list of options you can set for `sdkOptions`, see the | ||
| [JavaScript SDK options reference](../../../../../reference/sdk-js-options.md). | ||
|
|
||
| ### 4. Use the SDK | ||
|
|
||
| Use the SDK by using the `useSDK` hook in your React components. | ||
| For example: | ||
|
|
||
| ```js | ||
| import { useSDK } from '@metamask/sdk-react'; | ||
| import React, { useState } from 'react'; | ||
|
|
||
| export const App = () => { | ||
| const [account, setAccount] = useState<string>(); | ||
| const { sdk, connected, connecting, provider, chainId } = useSDK(); | ||
|
|
||
| const connect = async () => { | ||
| try { | ||
| const accounts = await sdk?.connect(); | ||
| setAccount(accounts?.[0]); | ||
| } catch(err) { | ||
| console.warn(`failed to connect..`, err); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <button style={{ padding: 10, margin: 10 }} onClick={connect}> | ||
| Connect | ||
| </button> | ||
| {connected && ( | ||
| <div> | ||
| <> | ||
| {chainId && `Connected chain: ${chainId}`} | ||
| <p></p> | ||
| {account && `Connected account: ${account}`} | ||
| </> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
| ``` | ||
|
|
||
| <details> | ||
| <summary>useSDK return values</summary> | ||
| <p> | ||
|
|
||
| - `sdk`: Main SDK object that facilitates connection and actions related to MetaMask. | ||
| - `connected`: Boolean value indicating if the dapp is connected to MetaMask. | ||
| - `connecting`: Boolean value indicating if a connection is in process. | ||
| - `provider`: The provider object which can be used for lower-level interactions with the Ethereum blockchain. | ||
| - `chainId`: Currently connected blockchain's chain ID. | ||
|
|
||
| </p> | ||
| </details> | ||
|
|
||
| The `sdk.connect()` method initiates a connection to MetaMask and returns an array of connected accounts: | ||
|
|
||
| ```javascript | ||
| const connect = async () => { | ||
| try { | ||
| const accounts = await sdk?.connect(); | ||
| setAccount(accounts?.[0]); | ||
| } catch(err) { | ||
| console.warn(`failed to connect..`, err); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| Refer to the [MetaMask JavaScript SDK examples](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples) | ||
| for advanced use cases. | ||
115 changes: 115 additions & 0 deletions
115
wallet/how-to/connect/set-up-sdk/javascript/react/react-ui.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| --- | ||
| sidebar_label: React UI | ||
| sidebar_position: 1 | ||
| --- | ||
|
|
||
| # Use MetaMask SDK with React UI | ||
|
|
||
| You can import [MetaMask SDK](../../../../../concepts/sdk.md) into your React dapp to enable your | ||
| users to easily connect to the MetaMask browser extension and MetaMask Mobile. | ||
| The `@metamask/sdk-react-ui` package not only exports hooks from [`@metamask/sdk-react`](index.md), | ||
| but also provides wrappers around [wagmi](https://wagmi.sh/) hooks and a basic UI button component | ||
| for connecting to MetaMask. | ||
|
|
||
| By combining the functions of `@metamask/sdk-react` and `@metamask/sdk-react-ui`, you can use both | ||
| the core functionality and the pre-styled UI components to streamline the integration of MetaMask | ||
| into your React dapp. | ||
|
|
||
| The SDK for React has the [same prerequisites](../index.md#prerequisites) as for standard JavaScript. | ||
|
|
||
| :::tip Examples | ||
| Refer to the [MetaMask JavaScript SDK examples](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples) | ||
| for advanced use cases. | ||
| ::: | ||
|
|
||
| ## Steps | ||
|
|
||
| ### 1. Install the SDK | ||
|
|
||
| In your project directory, install the SDK using Yarn or npm: | ||
|
|
||
| ```bash | ||
| yarn add @metamask/sdk-react-ui | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ```bash | ||
| npm i @metamask/sdk-react-ui | ||
| ``` | ||
|
|
||
| ### 2. Import the SDK | ||
|
|
||
| In your project script, add the following to import the SDK: | ||
|
|
||
| ```javascript | ||
| import { MetaMaskUIProvider } from '@metamask/sdk-react-ui'; | ||
| ``` | ||
|
|
||
| ### 3. Wrap your project with MetaMaskUIProvider | ||
|
alexandratran marked this conversation as resolved.
|
||
|
|
||
| Wrap your root component in a `MetaMaskUIProvider`. | ||
| For example: | ||
|
|
||
| ```js | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import App from './App'; | ||
| import { MetaMaskUIProvider } from '@metamask/sdk-react-ui'; | ||
|
|
||
| const root = ReactDOM.createRoot( | ||
| document.getElementById('root') as HTMLElement | ||
| ); | ||
|
|
||
| root.render( | ||
| <React.StrictMode> | ||
| <MetaMaskUIProvider sdkOptions={{ | ||
| dappMetadata: { | ||
| name: "Demo UI React App", | ||
| } | ||
| }}> | ||
| <App /> | ||
| </MetaMaskUIProvider> | ||
| </React.StrictMode> | ||
| ); | ||
| ``` | ||
|
|
||
| For the full list of options you can set for `sdkOptions`, see the | ||
| [JavaScript SDK options reference](../../../../../reference/sdk-js-options.md). | ||
|
|
||
| ### 4. Use the SDK | ||
|
|
||
| Use the SDK by using the `useSDK` hook in your React components. | ||
| See the [instructions for `@metamask/sdk-react`](index.md#4-use-the-sdk). | ||
|
|
||
| ### 5. Use the MetaMaskButton component | ||
|
|
||
| The `@metamask/sdk-react-ui` package provides a pre-styled button, `MetaMaskButton`, to initiate a | ||
| connection to MetaMask. | ||
| You can use it as follows: | ||
|
|
||
| ```js | ||
| import { MetaMaskButton } from "@metamask/sdk-react-ui"; | ||
| import React, { useState } from "react"; | ||
|
|
||
| export const App = () => { | ||
| return ( | ||
| <div className="App"> | ||
| <MetaMaskButton theme={"light"} color="white"></MetaMaskButton> | ||
| </div> | ||
| ); | ||
| }; | ||
| ``` | ||
|
|
||
| <details> | ||
| <summary>MetaMaskButton properties</summary> | ||
| <p> | ||
|
|
||
| - `theme`: Set to `light` or `dark` to adapt to your dapp's theme. | ||
| - `color`: The color of the button. Accepts any valid CSS color string. | ||
|
|
||
| </p> | ||
| </details> | ||
|
|
||
| Refer to the [MetaMask JavaScript SDK examples](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples) | ||
| for advanced use cases. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.