我试图在单击Next js中的按钮时显示一个对话框。
index.js
'use client';
export default function Test() {
const [
isDialogOpen,
setIsDialogOpen
] = useState(false);
return (
<>
<ButtonComponent
className={'flex items-center group'}
onButtonClick={() => { setIsDialogOpen(true);}}
type={ButtonType.SECONDARY}
>
<Resume className={'mr-2 duration-300 group-hover:stroke-white-default group-hover:dark:stroke-black-default scale-125 stroke-neongreen-default'} />
<span>
Open Dialog
</span>
</ButtonComponent>
<DialogComponent
open={isDialogOpen}
setIsOpen={setIsDialogOpen}
title={'Dialog open'}
>
<p>Hi, this is dialog</p>
</DialogComponent>
</>;
);
}
[tag:对话框. js]
'use client';
export default function DialogComponent(props: DialogProps) {
const {
children = null, open = false, title, setIsOpen
} = props;
return (
<div
className={'flex data-[open=false]:hidden overflow-auto w-full h-screen fixed top-0 left-0 bg-black-default/[.80] z-10 duration-300'}
data-open={isOpen}
ref={dialogRef}
>
<div className={'w-full h-screen md:min-h-full md:w-fit min-h-fit m-auto p-4'}>
<div className={'flex justify-between items-center'}>
<div className={'w-full'}>
<h1 className={'text-lg w-fit m-auto block'}>{title}</h1>
</div>
</div>
{children}
<div className={'w-full'}>
<ButtonComponent
className={'m-auto block mt-4'}
onButtonClick={() => setIsOpen(false)}
type={ButtonType.PRIMARY}
>
Close
</ButtonComponent>
</div>
</div>
</div>
);
}
问题:在index.js中,每当我点击按钮时,它不会更新组件的状态(isDialogOpen在devtools中仍然显示为false)。我不确定我在这里做错了什么。希望有人能帮助我!
谢谢!
- 请注意,我使用的是带有实验性应用程序目录的Next.JS 13。**
1条答案
按热度按时间hgqdbh6s1#
为了改变React中的状态,需要使用
setState()
函数。在示例中,该函数将在执行isDialogOpen = true
时使用。应该将其替换为setIsDialogOpen(true)
。