npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cxptek/web3client-react

v1.2.12

Published

![npm](https://img.shields.io/npm/v/%40cxptek%2Fweb3client)

Downloads

41

Readme

Web3Client React Components

npm

How to use?

Create clients

import { MetaMaskClient, TronLinkClient } from '@cxptek/web3client';

const metamaskConnector = new MetaMaskClient({});
const tronConnector = new TronLinkClient();

Add React components to your react app

import { ButtonConnect, Web3Provider } from '@cxptek/web3client-react';
import '@cxptek/web3client-react/dist/esm/style.css';

const options = {
  language: 'vi-VN',
  theme: 'dark', // 'dark' | 'light' | 'auto'
  connectors: [metamaskConnector, tronConnector],
  enforceSupportedChains: true // a popup will show if user in unsupported chain.
}
<Web3Provider options={options}>
  <ButtonConnect label="Connect" />
</Web3Provider>

Use react hook

Specs:

const {
  language, // component language - WIP
  open, // modal status
  setOpen, // Open modal
  route, // current page in modal
  setRoute, // set current page in modal
  errorMessage, // show Error - WIP
  connectorId, // connector ID
  setConnectorId, // set connector name | ID
  connectors, // list of connectors : Web3Client[]
  connect, // method to connect using web3client.connect
  account, // connected account
  isConnected,
  isDisconnected,
  disconnect, // disconnec current account
  client, // current connector
  web3Auth, // use web3Auth or not, require Web3AuthProvider context
} = useWeb3Context();
import { useWeb3Context} from '@cxptek/web3client-react';
const ComponentSample = () => {
  const {account, connect, disconnect} = useWeb3Context();

  return <div>{account ? 'Connected' : 'Connect'}</div>
}

Web3Auth Context

Signing in with Web3 context helps us to easily implement DApp authentication using Web3 with some methods:

Add react context

const web3AuthConfig = {
    // get message to sign
    getSignData: async (address: string): Promise<{nonce:string; message:string}> => {},
    // verify signed message
    verifyMessage: async (address: string, signature: string, nonce: string):Promise<boolean> => {},
    // get user session
    getMe: async ():Promise<Web3AuthSession> => {},
    // sign out
    signOut: async ():Promise<boolean> => {},
    // event after signed in
    onSignIn: (data: Web3AuthSession) => { },
    ...
  };
return (
<Web3AuthProvider {...web3AuthConfig}>
  <Web3Provider options={{ language: 'en-US', connectors: [metamaskConnector, tronConnector] }}>
      <ButtonConnect label="Connect" />
  </Web3Provider>
</Web3AuthProvider>
)

Usage

Use from ButtonConnect directly or use React Hooks:

import { useWeb3AuthContext, useWeb3Context } from '@cxptek/web3client-react';
import React, { FC } from 'react';

const SignOutButton: FC = () => {
  const { isSignedIn, signIn, signOut } = useWeb3AuthContext();
  const { client, account, connect, disconnect, connectors } = useWeb3Context();
  const handleAuth = async () => {
    let connected = true;
    if (!account || !client) {
      connected = await connect('MetaMask');
    }

    if (connected) {
      if (!isSignedIn) {
        // use default connector
        await signIn(connectors[0]);
      } else {
        await signOut();
        disconnect();
      }
    }
  };

  return (
    <button onClick={handleAuth} className={isSignedIn && account ? 'danger' : ''}>
      Custom {isSignedIn ? 'Sign Out' : account ? 'Sign In' : 'Connect and Sign In'}
    </button>
  );
};

export default SignOutButton;

Hooks

Specs:

const {
  isSignedIn
  data?, // user data
  status, // sign in status
  error?, // WIP
  isRejected,
  isError,
  isLoading,
  isSuccess,
  isReady,
  reset, // reset state
  signIn, // sign connected user in
  signOut
} = useWeb3AuthContext();

How to publish to NPM

Preparation

  • Ensure that all your code is on the main branch.
  • Make sure you have updated the version of your package in package.json.

There are 2 packages, and the publish package trigger will depend on the tag in the release.

The title and description field can be anything you want.

Finally, you just need to publish the release.