Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React函数组件的值捕获 #308

Open
xlearns opened this issue Apr 3, 2023 · 0 comments
Open

React函数组件的值捕获 #308

xlearns opened this issue Apr 3, 2023 · 0 comments

Comments

@xlearns
Copy link
Owner

xlearns commented Apr 3, 2023

import React, { useState, useRef } from "react";
import { useGetState } from "ahooks";

export default () => {
  const [count, setCount, getCount] = useGetState<number>(0);
  const [test, setTest] = useState(0);
  const lastValue = useRef(0);
  return (
    <button
      onClick={() => {
        setCount((count) => count + 1);
        setTest(test + 1);
        lastValue.current = test + 1;
        console.log("ref", lastValue.current, test);
        setTimeout(() => {
          console.log("ahook", getCount(), count);
        });
      }}
    >
      count: {count}
    </button>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant