image.png
    输入手机号后,发送验证码
    image.png

    1. import React, { useState, useRef, useEffect } from 'react';
    2. import { string, func, object, bool } from "prop-types";
    3. import { Input, Button, Space } from 'antd'
    4. VerifyCode.propTypes = {
    5. value: string,
    6. onChange: func,
    7. style: object,
    8. readyPost: bool,
    9. };
    10. function VerifyCode (props) {
    11. const { readyPost, ...inputProps } = props;
    12. const [lastTime, setLastTime] = useState(0);
    13. const timerRef = useRef(null);
    14. console.log(16, props)
    15. useEffect(() => {
    16. return () => {
    17. if (timerRef.current) {
    18. clearTimeout(timerRef.current)
    19. }
    20. }
    21. }, [])
    22. function counting(time = 60): void {
    23. console.log('time60', time)
    24. if (time < 0) return
    25. setLastTime(time)
    26. console.log(29, timerRef.current, 'time', time)
    27. timerRef.current = setTimeout(() => {
    28. counting(time - 1)
    29. }, 1000)
    30. }
    31. console.log('lastItme', lastTime)
    32. return (
    33. <Space>
    34. <Input
    35. {...inputProps}
    36. style={{ marginRight: 16, ...inputProps.style }}
    37. />
    38. {lastTime === 0 && (
    39. <Button
    40. disabled={!readyPost}
    41. block
    42. onClick={() => counting()}
    43. >
    44. 发送验证码
    45. </Button>
    46. )}
    47. {lastTime > 0 && <span>剩余{lastTime} </span>}
    48. </Space>
    49. )
    50. }
    51. export default VerifyCode;