You define state inside the hook and return setValue and value from the hook, just like useState does you don't pass state to hook.
const useTimer = () => { const [count, setCount] = useState(0); function increment() { setCount(count+1); } function decrement() { setCount(count-1); } return {count, increment, decrement}; };
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.
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}; };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You define state inside the hook and return setValue and value from the hook, just like useState does you don't pass state to hook.
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.
In hooks props are often used for state initialization: