0.30.0 • Published 6 months ago

@metamask/sdk-react v0.30.0

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

MetaMask SDK React

The MetaMask SDK React allows developer an easier integration to the MetaMask SDK on React-based apps.

Getting Started

Install the SDK:

yarn add @metamask/sdk-react

Wrap your application in the provider

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>
);

Use the SDK in your components:

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>
  );
};

Look for more examples at https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples

Contacts

Contact the MetaMask SDK team for a complimentary design optimization workshop here

0.30.0

6 months ago

0.29.0

6 months ago

0.29.2

6 months ago

0.29.1

6 months ago

0.27.0

9 months ago

0.28.1

8 months ago

0.26.3

10 months ago

0.28.0

8 months ago

0.26.2

10 months ago

0.26.1

10 months ago

0.26.0

10 months ago

0.20.5

11 months ago

0.20.4

11 months ago

0.28.4

7 months ago

0.28.3

7 months ago

0.26.5

9 months ago

0.28.2

7 months ago

0.26.4

10 months ago

0.20.2

11 months ago

0.19.0

12 months ago

0.18.6

1 year ago

0.18.5

1 year ago

0.18.4

1 year ago

0.18.0

1 year ago

0.17.2

1 year ago

0.17.0

1 year ago

0.17.1

1 year ago

0.16.0

1 year ago

0.15.0

1 year ago

0.14.2

1 year ago

0.10.0

2 years ago

0.11.0

1 year ago

0.10.1

1 year ago

0.9.0

2 years ago

0.5.4

2 years ago

0.12.0

1 year ago

0.11.1

1 year ago

0.8.0

2 years ago

0.6.2

2 years ago

0.5.3

2 years ago

0.13.0

1 year ago

0.12.1

1 year ago

0.11.2

1 year ago

0.5.6

2 years ago

0.14.0

1 year ago

0.12.2

1 year ago

0.14.1

1 year ago

0.12.3

1 year ago

0.12.4

1 year ago

0.7.0

2 years ago

0.6.1

2 years ago

0.3.1

2 years ago