diff --git a/chapter3/src/app/demo/_components/ref.tsx b/chapter3/src/app/demo/_components/ref.tsx new file mode 100644 index 0000000..0650011 --- /dev/null +++ b/chapter3/src/app/demo/_components/ref.tsx @@ -0,0 +1,31 @@ +// src/app/demo/_components/ref.tsx +'use client'; + +import { type FC, useEffect, useState, useRef } from 'react'; +import { Button } from '@/app/_components/shadcn/ui/button'; +import clsx from 'clsx'; +import $styles from './style.module.css'; + +const RefDemo: FC = () => { + const [count, setCount] = useState(0); + const inited = useRef(count); + useEffect(() => { + if (inited.current !== count) { + inited.current = count; + console.log('changed'); + } + }, [count]); + return ( +
+

useRef Demo

+

{count}

+
+ +
+
+ ); +}; + +export default RefDemo;