2.0.2 • Published 2 years ago

@asmundwien/more-hooks v2.0.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

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

#requiredtypedescription
1trueasync FunctionThe method that this hook will call
2falseUseAsyncOptionsOptions object. See documentation on UseAsyncOptions below.

Types

UseAsyncResponse

nametypedescription
pendingbooleanWhether or not the asynchronous call is currently pending.
responseTThe response from the asynchronous call. T is the same response type as the asynchronos method the hook was initialized with.
callasync FunctionWill initialize a call to the method that the hook was initialized with. Will expect the same input parameters as the original method definition.
errorunknownWhatever exception the asynchronous call may have thrown.
successbooleanWhether or not the asynchronous call was completed without catching any errors.
hasBeenCalledbooleanDefaults 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

#typedescription
restFunctionMethods 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.

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago