Coder Social home page Coder Social logo

aldrinpvincent / react-use-custom-hooks Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 0.0 5.31 MB

A collection of react custom hooks in typescript.

Home Page: http://react-use-custom-hooks.vercel.app

License: MIT License

TypeScript 100.00%
hooks react-custom-hook react-hooks reactjs typescript

react-use-custom-hooks's Introduction

react-use-custom-hooks's People

Contributors

aldrinpvincent avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

react-use-custom-hooks's Issues

useAsync is untyped

useAsync is untyped. Um, I don't have a pull request but here is one of mine that you could could probably crib from.

export const useAsync = <S, T>(asyncFn: (parametersObject: S) => Promise<T>, parametersObject: S) => {
  const [result, setResult] = useState<[T?, boolean?, Error?]>([undefined, true, undefined]);
  const newInput = useMemo<S>(() => parametersObject, Object.values(parametersObject));
  const changedInput = useDebounce(newInput, 100);

  const reload = useCallback(() => {
    setResult([result[0], true, undefined]); // don't clear out Data on new call; makes UX less jumpy
    asyncFn(changedInput)
      .then(data => setResult([data, false, undefined]))
      .catch(err => setResult([undefined, false, err]));
  }, [changedInput, asyncFn]);

  useEffect(reload, [changedInput]); // don't auto-call if asyncFn changed cause it might be an inline lambda

  return (result as any[]).concat(reload) as [T | undefined, boolean, Error | undefined, () => void];
};

usage: const [response, isLoading, error, reload] = useAsync(fetchData, { columns, sorting, paging, searchText });

It has a smaller featureset than yours and the invocation is less verbose, and I never truly solved the typing issue on the return statement, but from the outside it's nice. The dependency array is an object instead of an array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.