react icon indicating copy to clipboard operation
react copied to clipboard

Create a new react hook called prestate

Open umerprogramm opened this issue 2 years ago • 7 comments

I want to add a new react hook called prestate hook this hook will allow accessing previous values that the user assigned I'm going to work on it if someone is interested in it then please leave a comment

umerprogramm avatar Sep 16 '22 15:09 umerprogramm

i am interested to work on it but i have no experience in opensource

DIWAKARKASHYAP avatar Sep 17 '22 18:09 DIWAKARKASHYAP

what do you mean by pre-state. when you are going to set state, as you know setState accepts a function as value which react pass pre value of state as argument to it and it returns new state. there you have pre-state. if you need more, please explain more with the api you mean

amirqasemi74 avatar Sep 18 '22 10:09 amirqasemi74

Wouldn't this work, assuming I'm not missing the point.

function usePrevState(value) {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  }, [value]);
  return ref.current;
}

const [value, setValue] = useState(1);
const prevValue = usePrevState(value);

matvp91 avatar Sep 19 '22 12:09 matvp91

usePrevious is a well known custom hook and I assume it is what you're asking for. I'm not sure there's a place for it in core though as it has a specific use case and not something that would be used in every application unlike those already part of react core.

varwasabi avatar Sep 20 '22 07:09 varwasabi

Even I am also interested but have zero experience in open-source. varwasabi commented use previous and also alternative options.

madhusudhan123-star avatar Sep 26 '22 02:09 madhusudhan123-star

Hello Everyone! Thanks for making your interest in it. So a lot of guys are interested, But they not understanding what I'm actually trying to say. prestate Hook is just not going to be Hook that contains the previous values but it also contains some cool methods Like:

  • Binary Search
  • sorting Arrays
  • Reduce the repeated elements from the array And much more. (If you have an idea feel free to leave a comment I will definitely put that on my list)

I will soon put a code example that helps to visualize what I'm trying to say.

umerprogramm avatar Sep 26 '22 15:09 umerprogramm

@umerprogramm if you need some things like that, maybe you can write it. But I think this place is not the correct to report that. Thanks mate! 👍

damianpumar avatar Oct 22 '22 10:10 damianpumar

Hello Everyone I created the hum state Hook repo It is a great opportunity to contribute to open source

link : Humstate

umerprogramm avatar Dec 01 '22 11:12 umerprogramm