All Projects → mdtanrikulu → use-metamask

mdtanrikulu / use-metamask

Licence: MIT License
a custom React Hook to manage Metamask in Ethereum ĐApp projects

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to use-metamask

useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (+34.35%)
Mutual labels:  hooks, react-hooks
flhooks
React like Hooks implementation for Flutter.
Stars: ✭ 38 (-70.99%)
Mutual labels:  hooks, react-hooks
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-84.73%)
Mutual labels:  hooks, react-hooks
Fre
👻 Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+2338.93%)
Mutual labels:  hooks, react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (-61.83%)
Mutual labels:  hooks, react-hooks
NFT-Dapp-Boilerplate
A highly scalable NFT and DEFI boilerplate with pre added web3 and different wallets with a focus on performance and best practices
Stars: ✭ 51 (-61.07%)
Mutual labels:  metamask, react-hooks
react-breakpoints
Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer "web design's unicorn" 🦄
Stars: ✭ 74 (-43.51%)
Mutual labels:  hooks, react-hooks
Awesome React Hooks
Awesome React Hooks
Stars: ✭ 7,616 (+5713.74%)
Mutual labels:  hooks, react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (-50.38%)
Mutual labels:  hooks, react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (-85.5%)
Mutual labels:  hooks, react-hooks
React Form
⚛️ Hooks for managing form state and validation in React
Stars: ✭ 2,270 (+1632.82%)
Mutual labels:  hooks, react-hooks
use-detect-print
A react hook to detect when a page is being printed
Stars: ✭ 55 (-58.02%)
Mutual labels:  hooks, react-hooks
Haunted
React's Hooks API implemented for web components 👻
Stars: ✭ 2,197 (+1577.1%)
Mutual labels:  hooks, react-hooks
openzeppelin-network.js
An easy to use and reliable library that provides one line access to Web3 API.
Stars: ✭ 45 (-65.65%)
Mutual labels:  metamask, react-hooks
Graphql Hooks
🎣 Minimal hooks-first GraphQL client
Stars: ✭ 1,610 (+1129.01%)
Mutual labels:  hooks, react-hooks
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (-70.23%)
Mutual labels:  hooks, react-hooks
The Platform
Web. Components. 😂
Stars: ✭ 4,355 (+3224.43%)
Mutual labels:  hooks, react-hooks
Formik
Build forms in React, without the tears 😭
Stars: ✭ 29,047 (+22073.28%)
Mutual labels:  hooks, react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+87.79%)
Mutual labels:  hooks, react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-90.08%)
Mutual labels:  hooks, react-hooks

Contributors Forks Stargazers Issues MIT License LinkedIn Codecov


Logo

useMetamask React Hook

An awesome React Hook to jumpstart your projects!
Explore the docs » (Not ready yet)

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

About The Project

Product Name Screen Shot

There are many great Web3 tools available on GitHub, however, I didn't find one that really suited my needs, so I created this simple React Hook. I want to connect to Metamask as quick as possible from any component, without rewriting all the network and account change logic. -- I think this is it.

Here's why:

  • Your time should be focused on creating your project. A project that solves a problem and helps others
  • You shouldn't be implementing the same logic over and over from scratch
  • You shouldn't need enormous tool boxes when you only want a connection to the wallet.

Of course, no single hook will serve all projects since your needs may be different. So I'll be adding more in the near future. You may also suggest changes by forking this repo and creating a pull request or opening an issue. Thanks in advance to all the people who wants to put more into the project!

Getting Started

Install via npmjs

Easiest option is installing the package from npm with;

# via npm
npm i use-metamask
# or via yarn
yarn add use-metamask

But if you would like to build a package locally, follow this below;

Build from source

You'll need some prerequisites in order to be able build the package.

  • npm > 12.13.0 (best installing via nvm)
    npm install npm@latest -g
  • yarn (optional)
    https://classic.yarnpkg.com/en/docs/install

Installation

  1. Clone the repo
    git clone https://github.com/mdtanrikulu/use-metamask.git
  2. Install NPM packages
    # via npm
    npm install
    # via yarn
    yarn
  3. Build the Package
    # via npm
    npm run build
    # via yarn
    yarn build
  4. Prepare Tar Package
    npm pack
    # now the package is ready to use, you can simply do "npm i ./pathoftarfile/use-metamask-1.0.0.tgz" in your project

Usage

  1. Wrap your App component with MetamaskStateProvider
import React                     from 'react';
import ReactDOM                  from 'react-dom';
import { MetamaskStateProvider } from "use-metamask";
import App                       from './App';

ReactDOM.render(
    <MetamaskStateProvider>
      <App />
    </MetamaskStateProvider>
  document.getElementById('root')
);
  1. Import your hook to your App component
import { useEffect, useState } from "react";
import { useMetamask }         from "use-metamask";
// you can use any web3 interface
// import { ethers }           from "ethers";
// import Web3                 from "web3";

function App() {
  const { connect, metaState } = useMetamask();
  //...
  1. Call connect async method with your favorite Web3Interface library
//...
function App() {
  const { connect, metaState } = useMetamask();

  // instead of calling it from useEffect, you can also call connect method from button click handler
  useEffect(() => {
    if (!metaState.isConnected) {
      (async () => {
        try {
          await connect(Web3);
        } catch (error) {
          console.log(error);
        }
      })();
    }
  }, []);
  1. Now you can reach all information under metaState object; (they will be updated in case of any change in metamask)
// all connected Metamask accounts 
// account: Array [ "0x68bbaeb98ac22e9e6516fb35c8d27ded05bc0607" ]

// current connected chain id and name 
// chain: Object { id: "4", name: "rinkeby" }

// shows if Metamask Extension is whether exist or not in the user's browser
// isAvailable: true

// shows if connection is established with at least one metamask account
// isConnected: true

// web3 instance of Web3 interface you provided
// web3: Object { _isProvider: true, anyNetwork: true, _maxInternalBlockNumber: -1024, … }

Note: If you would like to check if Metamask is already connected to your dapp or not, you can call getAccounts method beforehand.

You can also get chain information by calling getChain method, without the need to call the connect method.

const { connect, getAccounts, getChain, metaState } = useMetamask();

useEffect(() => {
    if (metaState.isAvailable) {
      (async () => {
        try {
          /* you can get the information directly 
           * by assigning them to a variable, 
           * or from metaState.account and metaState.chain 
          */
          let account = await getAccounts();
          let chain = await getChain();
        } catch (error) {
          console.log(error);
        }
      })();
    }
  }, []);

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Muhammed Tanrikulu - @tanrikuIu - [email protected]

Project Link: https://github.com/mdtanrikulu/use-metamask

Acknowledgements

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].