DEV Community

Discussion on: Why doesn't useState has a dependency array?

 
swadhwa16 profile image
Sumit Wadhwa

yes I think you're right. a hook's state should not depend on its props. it should be private and it should expose functions that trigger hook's state change so that we have more control over hook's state. thanks.

Thread Thread
 
peerreynders profile image
peerreynders • Edited

In hooks props are often used for state initialization:

const useCounter = (initialCount = 0) => {
   const [count, setCount] = useState(initialCount);
   function increment() {
     setCount(count+1);
   }
   function decrement() {
     setCount(count-1);
   }
   return {count, increment, decrement};
};
Enter fullscreen mode Exit fullscreen mode