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

@rozhkov/react-useful-hooks

v1.0.9

Published

Useful hooks for React application

Downloads

11,767

Readme

🔥 React Useful Hooks 🔥

This library aims to provide the most necessary hooks, which are required in typical React or React Native app;

Installation

For npm

npm install @rozhkov/react-useful-hooks

For yarn

yarn add @rozhkov/react-useful-hooks

Navigation

Hooks

useInit

Initializes a value during component mounting and returns it each time throughout the component lifecycle.

Example

// first render
const initialized = useInit(() => {}); // the callback is called.

// after
const initialized = useInit(() => {}); // the callback is not called, the return value is not changed.

Interface

<T>(callback: () => T) => T;

useIsFirstRender

At first render, the result is true, then false.

Example

// first render
const isMouting = useIsFirstRender(); // The return is true.

// after
const isMouting = useIsFirstRender(); // The return is false.

Interface

() => boolean;

usePrevious

Returns a previous argument that was passed during a previous render.

Example

// first render
const value = usePrevious('My arg'); // The first result is always undefined.

// second render
const value = usePrevious('Not my arg'); // The return is 'My arg'.

Interface

<T>(arg: T) => T | undefined;

useStableCallback

Returns a new callback that preserves the reference between renderers. If you call a function, the last function that was passed to the argument will be called.

Example

// first render
const wrapped = useStableCallback(() => 'Some function');
wrapped(); // 'Some function'

// second render
const wrapped = useStableCallback(() => 'So, I have new function');
wrapped(); // 'So, I have new function', but current 'wrapped' === previous 'wrapped'.

Interface

<T extends AnyFunc>(callback: T | null | undefined) => T;

useMemoObject

Return a memoized object, comparing values of its keys.

Example

const memoizedObj = useMemoObject({
  fieldValue1,
  fieldValue2,
  ...otherFields
});

// is equal
const memoizedObj = useMemo(() => ({
  fieldValue1,
  fieldValue2,
  ...otherFields
}), [
  fieldValue1,
  fieldValue2,
  ...otherFields
]);

Interface

<T extends object>(obj: T) => T;

useMemoArray

Return a memoized array, comparing its values.

Example

const memoizedArray = useMemoArray([
  value1,
  value2,
  ...otherValues
]);

// is equal
const memoizedArray = useMemo(() => ([
  value1,
  value2,
  ...otherValues
]), [
  value1,
  value2,
  ...otherValues
]);

Interface

<T extends any[]>(array: T) => T;

useStateRef

Similar to useState, but it also returns a third item "ref" with the most recent value.

Example

const [, setValue, valueRef] = useStateRef(0);

useEffect(() => {
  valueRef.current; // 0
  setValue(1);
  valueRef.current; // 1
}, []);

Interface

<S = undefined>() => [S | undefined, Dispatch<SetStateAction<S | undefined>>, Ref<S | undefined>];
<S>(initialState: S | (() => S)) => [S, Dispatch<SetStateAction<S>>, Ref<S>];

useIsChanged

Returns the result of comparing between a current argument and a previous argument.

Example

// first render
const value = useIsChanged(0); // The return is false

// second render
const value = useIsChanged(0); // The return is false
// or
const value = useIsChanged(1); // The return is true

Interface

(value: any) => false;

useArgByRef

Returns 'ref' with the most recent value which was passed to the hook.

Example

// first render
const ref = useArgByRef(0);
ref.current; // 0

// second render
const ref = useArgByRef([]);
ref.current; // []

Interface

<T>(value: T) => {readonly current: T};

useChangeCounter

Returns the count of how many times the argument has changed throughout the component lifecycle. This can be helpful when you have complex conditions in useEffect, etc.

Example

// first render
const count = useChangeCounter('init'); // The return is 0

// second render
const count = useChangeCounter('init'); // The return is 0
// or
const count = useChangeCounter('changed'); // The return is 1

Interface

<T>(value: T, compare?: (v1: T, v2: T) => boolean) => number;

👨‍💻 Author

Sergey Rozhkov

🎯 Was it helpful?

Do you like it and find it helpful? You can help this project in the following way:

  • ⭐ Put the star.
  • 💡 Suggest your ideas.
  • 😉 Open a founded issue.

📄 License

Rozhkov React Useful Hooks is MIT licensed, as found in the LICENSE file.