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

@kaungthantzindev/hooks

v1.3.0

Published

A collection of custom React hooks

Downloads

28

Readme

@kaungthantzindev/hooks

A versatile collection of custom React hooks crafted with TypeScript, designed to enhance and simplify your React applications.

Installation

Install the hooks library with npm:

npm install @kaungthantzindev/hooks

Or with yarn:

yarn add @kaungthantzindev/hooks

Available Hooks

  • useHashState: Synchronize your component's state with the browser's URL hash.
  • useGoogleLogin: A simple hook to integrate Google login with support for id_token and access_token retrieval.
  • useAppleLogin: Simplify Apple login integration, supporting both authorization code and id_token responses.

Usage

Import and use a hook in your React component:

useHashState

This hook synchronizes a React state with the URL's hash, making it perfect for scenarios where you need the state to be reflected and controlled via the URL.

import { useHashState } from "@kaungthantzindev/hooks";

function MyComponent() {
  const [value, setValue, clearValue] = useHashState("myKey", {
    initialValue: "default",
    encode: (val) => btoa(val), // Base64 encode
    decode: (val) => atob(val), // Base64 decode
    debounce: 300,
    syncWithSessionStorage: true,
    onHashChange: (newValue, oldValue) => {
      console.log("Hash changed from", oldValue, "to", newValue);
    },
    errorHandler: (error) => {
      console.error("Error with hash state:", error);
    },
  });

  return (
    <div>
      <p>Value: {value}</p>
      <button onClick={() => setValue("newValue")}>Set New Value</button>
      <button onClick={clearValue}>Clear Value</button>
    </div>
  );
}

useGoogleLogin

useGoogleLogin is an easy-to-use hook that brings Google login functionality to your React app. It handles both id_token and access_token retrieval, making the authentication process smooth and straightforward.

import { useGoogleLogin } from "@kaungthantzindev/hooks";

function LoginComponent() {
  const { initiateLogin, isLoading } = useGoogleLogin({
    clientId: "YOUR_GOOGLE_CLIENT_ID",
    redirectUri: "http://localhost:3000",
    onSuccess: (idToken, accessToken) => {
      console.log("Login successful!", { idToken, accessToken });
    },
    onError: (error) => {
      console.error("Login failed:", error);
    },
  });

  return (
    <div>
      <button onClick={initiateLogin} disabled={isLoading}>
        Login with Google
      </button>
      {isLoading && <p>Loading...</p>}
    </div>
  );
}

useAppleLogin

useAppleLogin simplifies integrating Apple login into your React app. It helps retrieve authorization codes or ID tokens for authentication.

import { useAppleLogin } from "@kaungthantzindev/hooks";

function AppleLoginComponent() {
  const { initiateLogin, isLoading } = useAppleLogin({
    clientId: "YOUR_APPLE_CLIENT_ID",
    redirectUri: "http://localhost:3000",
    onSuccess: (code, idToken) => {
      console.log("Login successful!", { code, idToken });
    },
    onError: (error) => {
      console.error("Login failed:", error);
    },
  });

  return (
    <div>
      <button onClick={initiateLogin} disabled={isLoading}>
        Login with Apple
      </button>
      {isLoading && <p>Loading...</p>}
    </div>
  );
}

Features

  • Flexibility: Each hook is built to be flexible, allowing you to integrate it smoothly into your existing React application.
  • TypeScript Support: Full TypeScript support ensures that you can leverage strong typing and reduce runtime errors.
  • Easy Integration: Designed to be easy to integrate and use with comprehensive examples and documentation.

Contributing

Contributions are always welcome! Please read the contributing guide on GitHub to learn how you can contribute to this project.

Running Tests

To run tests, install the package and its dependencies, and run the following command:

npm test

Building

To build the project, run:

npm run build

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Kaung Thant Zin - [email protected]

Keywords

  • React
  • Hooks
  • hooks collection
  • useHashState
  • useGoogleLogin
  • useAppleLogin

Repository

View the project repository here:

GitHub Repository

Thank you for using or contributing to this library!