It’s easy to write the timer in JS, but the hooks are different. Here I have a component that I can copy directly from

/ / verifyCode subcomponents

let timer = null
const VerifyCode = ({ onClick }: IProps) = > {
  const [time, setTime] = useState(0)

  useEffect(() = > {
    timer && clearInterval(timer);
    return () = > timer && clearInterval(timer); } []); useEffect(() = > {
    if( time === 60 ) timer = setInterval(() = > setTime(time= > --time), 1000)
    else if ( time === 0 ) clearInterval(timer)
  }, [time])

  const getCode = () = > {
    // Used as a component
    onClick && onClick(() = > {
      setTime(60)})// Use it directly
    setTime(60)}return (
    <View onClick={getCode} className=' '>{ time? '${time} after seconds' :' Get verification code '}</View>)}export default VerifyCode;
Copy the code

// Parent const getCode = (call) => {call && call()} <VerifyCode onClick={getCode} />Copy the code