mediamonks / react-kit

A collection of React hooks, components and utilities we use at Media.Monks
https://mediamonks.github.io/react-kit/
MIT License
9 stars 2 forks source link

Create useAsyncValue hook #233

Open leroykorterink opened 1 year ago

leroykorterink commented 1 year ago
import { useBeforeMount } from '@mediamonks/react-hooks';
import { useState } from 'react';

export function useAsyncValue<T extends () => any>(initializeFunction: T): Awaited<ReturnType<T>> | undefined {
  const [value, setValue] = useState<Awaited<ReturnType<T>> | undefined>();

  useBeforeMount(async () => {
    setValue(await initializeFunction());
  });

  return value;
}
Franqsanz commented 1 year ago

Hello, is it copying that same example and saving it in a folder with the same name?

ThaNarie commented 1 year ago

@Franqsanz There are a few more steps to it (docs, tests, etc). You can check the readme to see what's needed to contribute a new hook to the repo: https://github.com/mediamonks/react-hooks#development