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

how to use setXXXX from useState in callBack

$
0
0

I am not really sure where I did wrong. I have one customized Hook that called with providing inputValue. and one local variable value initial Value is 0. so after clicking button from component level, onNumberChange hook triggered and if value and input value is not the same. it should update value under hook by setValue. However, after I place the debug under if (isNumberUpdate) block. I can see value stay the same as 0 if I clicking the button twice.

export function useNumberChange(inputValue) {  const [value, setValue] = useState(0)  const onNumberChange = useCallback(async (): Promise<void> => {    const isNumberUpdate = isNumberDiff(inputValue, value);    if (isNumberUpdate) {      //console.log('debug')      //console.log(value, inputValue)      setValue(inputValue);    }  }, [inputValue, value]);//componentexport const Home: FC<{update:number}> = (props) => {const {updateValue} = props  const {onNumberChange} = useNumberChange(updateValue)  const handleOnClick = useCallBack(()=>{    onNumberChange()  })  return (<div className="App"><button onClick={handleOnClick}>click</button></div>  );}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>