'use client'; import { Button } from 'antd'; import { type FC, useContext, useEffect, useRef, useState } from 'react'; import { LoacalContext, locales } from './context/constants'; import $styles from './style.module.css'; const StateDemo2: FC = () => { const { local, setLocal } = useContext(LoacalContext); const [ghost, setGhost] = useState(false); const [width, setWidth] = useState(0); const toggleGhostBtn = () => setGhost(!ghost); const resizeHandle = () => setWidth(window.innerWidth); const booleans = useRef(true); const changeLocal = () => { booleans.current = !booleans.current; setLocal(locales[booleans.current ? 1 : 0]); }; useEffect(() => { if (typeof window !== 'undefined') { setWidth(window.innerWidth); window.addEventListener('resize', resizeHandle); } return () => window.removeEventListener('resize', resizeHandle); }); useEffect(() => { console.log('切换幽灵按钮'); }, [ghost]); return (
{local.label}

useEffect Demo

{ghost ? 'ghost' : '普通'}按钮

宽度为: {width}

); }; export default StateDemo2;