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

@asmundwien/more-hooks

v2.0.2

Published

A collection of usefull hooks that are commonly used in React development. All credits to the great community for coming up with and sharing these ideas with the world.

Downloads

11

Readme

more-hooks

A collection of usefull hooks that are commonly used in React development.

We take no credits for these components as they are merely a collection of ideas shared accross the community. Thanks for being great!

This library is written with full TypeScript support.

Table of content

useAsync

This hook is designed to simplify the handling of a asynchronous calls.

Example

import { useAsync } from "@asmundwien/more-hooks";

const App = () => {
  const { pending, error, response, call } = useAsync(someAsyncFunc);
  return (
    <>
      {pending && <LoadingSpinner />}
      {error && <MyError error={error} />}
      {response && <MyData data={response} />}
      <Button onClick={() => call()}>Fetch something</Button>
    </>
  );
};

Input arguments

| # | required | type | description | | --- | -------- | ----------------- | ----------------------------------------------------------- | | 1 | true | async Function | The method that this hook will call | | 2 | false | UseAsyncOptions | Options object. See documentation on UseAsyncOptions below. |

Types

UseAsyncResponse

| name | type | description | | ------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | | pending | boolean | Whether or not the asynchronous call is currently pending. | | response | T | The response from the asynchronous call. T is the same response type as the asynchronos method the hook was initialized with. | | call | async Function | Will initialize a call to the method that the hook was initialized with. Will expect the same input parameters as the original method definition. | | error | unknown | Whatever exception the asynchronous call may have thrown. | | success | boolean | Whether or not the asynchronous call was completed without catching any errors. | | hasBeenCalled | boolean | Defaults to false. Will remain true once the asynchronous call has been made at at least once. |

UseAsyncOptions

Object containing optional pramaters. | name | type | description | | ------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | | immediate | tuple | A list with the same parameters as the call-method. Will initialize a call when the component mounts. |

useOnMount

This tiny hook will run only during your components first render lifecycle. Remeber componentDidMount()?.

Example

import { useOnMount } from "@asmundwien/more-hooks";

const Component = () => {
  const [text, setText] = useState("");

  const fetch = async () => {
    const response = await myApi();
    setText(response);
  };

  useOnMount(fetch);

  return (
    <>
      {text && <p>Response text: {text}</p>}
      {!text && <Alert>Just mounted anew, hang on...</Alert>}
    </>
  );
};

Input arguments

| # | type | description | | ---- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | rest | Function | Methods that will be called when the component mounts. Because it is a rest parameter any number of methods can be passed. |

createDefinedContext

An implementation on top of React Context. It ensures the existance of your ContextProvider and its value. The content of your context can therefore never be undefined.

Example

import { createDefinedContext, useOnMount } from "@asmundwien/more-hooks";

export const [useUserContext, UserProvider] = createDefinedContext<User>();

const UserContext = ({ children }: Props) => {
  const [user, setUser] = useState<User>();
  useOnMount(() => getUser().then(setUser));

  if (!user) {
    return <Loading />;
  }

  return <UserProvider value={user}>{children}</UserProvider>;
};
  1. Create a new defined context.
  2. Get your data from somewhere, then feed it to your provider. In this example it calls for an async user object.
import { UserContext, UserDetails } from ".";

const App = () => (
  <UserContext>
    <UserDetails />
  </UserContext>
);

Any children of your context will have access to the data in the context provider.

import { useUserContext } from "UserContext";

const UserDetails = () => {
  const { name } = useUserContext();
  return <p>User name: {name}</p>;
};

Finally, access the hook from your DefinedContext.

Input

In a TypeScript-project, a Type-definition is required.

Return

Returns a tuple containing a hook to access your data, and the React Context Provider needed to initialize the context.