import { useTransition, useState, useCallback } from "react";
import "./index.css";
function getArray(str?: string) {
const arr: (number | string)[] = [str ?? ""];
for (let i = 0; i < 1000000; i++) {
arr.push(Math.random() * 100);
}
return arr;
}
const PUseTransition = function PUseTransition_() {
const [isPadding, startTransition] = useTransition();
const [tab, setTab] = useState(getArray("default"));
const changeTab = useCallback((key?: string) => {
startTransition(() => {
setTab(getArray(key));
});
}, []);
return (
<div className="page-example">
Example
<div>
<button
type="button"
onClick={() => {
changeTab("k1");
}}>
修改Tab为K1
</button>
<button
type="button"
onClick={() => {
changeTab("k2");
}}>
修改Tab为K2
</button>
<button
type="button"
onClick={() => {
changeTab("k3");
}}>
修改Tab为K3
</button>
</div>
{isPadding ? "loading" : tab.join("-")}
</div>
);
};
export default PUseTransition;