Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

How to get the return value of setState(prev => (prev)) in React?

$
0
0

I have a custom hook which wraps setState function so I can do something with the values as they are passed into the setter.

In this case, setState either takes a string value or a callback with the prev value of the state:

setState((prev) => (prev +' world')); // hello world
const useHook = () => {   const [stateValue, setStateValue] = useState('hello');   const setWrapper = (value: string | (prev: string) => string) => {      // how can I get the result of (prev) => string here      // aka 'hello world' value.   }   return [stateValue, setWrapper]}

How can I get the return value passed to setWrapped if a callback with prev was used?


Viewing all articles
Browse latest Browse all 6290

Trending Articles