An awesome React Hook to jumpstart your projects!
Explore the docs » (Not ready yet)
View Demo
·
Report Bug
·
Request Feature
[]()
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:
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!
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;
You'll need some prerequisites in order to be able build the package.
npm install npm@latest -g
https://classic.yarnpkg.com/en/docs/install
git clone https://github.com/mdtanrikulu/use-metamask.git
# via npm
npm install
# via yarn
yarn
# via npm
npm run build
# via yarn
yarn build
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
import React from 'react';
import ReactDOM from 'react-dom';
import { MetamaskStateProvider } from "use-metamask";
import App from './App';
ReactDOM.render(
document.getElementById('root') );
2. Import your hook to your App component
```JS
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();
//...
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);
}
})();
}
}, []);
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.
```js
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);
}
})();
}
}, []);
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Distributed under the MIT License. See LICENSE
for more information.
Muhammed Tanrikulu - @md_tanrikulu - md.tanrikulu@gmail.com
Project Link: https://github.com/mdtanrikulu/use-metamask