使用MOBX状态时,ANTD模态不打开 /关闭
我有一个简单的MOBX商店,该存储具有Bool VAR ContactFormopen和函数以设置SetContactformopen。在单击模式外或取消时,它可以正常工作,我可以看到它会改变,但模态不会打开或关闭。
const handleCancel = (e: any) => {
e.stopPropagation();
store.setContactFormOpen(false);
};
return (
<Modal title="Contact us" visible={store.contactFormOpen} width={740} centered footer={null} onCancel={(e) => handleCancel(e)}>
<h1>Contact form</h1>
</Modal>
)
在控制台日志中,我可以看到store.contactformopen
正在从真实变为false,但模式未关闭或打开。有什么想法吗?谢谢!
商店:
export default class store {
contactFormOpen = false;
constructor() {
makeAutoObservable(this)
}
setContactFormOpen = (isOpen: boolean) => {
this.contactFormOpen = isOpen;
}
}
I have a simple MobX store that has a bool var contactFormOpen and function to set setContactFormOpen. It is working when clicking outside the modal or on cancel I can see that it changes but the modal doesn't open or close.
const handleCancel = (e: any) => {
e.stopPropagation();
store.setContactFormOpen(false);
};
return (
<Modal title="Contact us" visible={store.contactFormOpen} width={740} centered footer={null} onCancel={(e) => handleCancel(e)}>
<h1>Contact form</h1>
</Modal>
)
In the console log I can see that store.contactFormOpen
is changing from true to false, but the modal is not closing or opening. Any ideas? Thanks!
The store :
export default class store {
contactFormOpen = false;
constructor() {
makeAutoObservable(this)
}
setContactFormOpen = (isOpen: boolean) => {
this.contactFormOpen = isOpen;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从描述中很难理解您的应用程序的样子,但是很明显,您至少错过了一个重要的事情:要了解组件的更改,必须将其包裹在观察者中。
我大约重现了您的应用程序,这是一个链接,您可以在其中看到应该如何实现它:
codesandbox链接&gt;
It's hard to understand what your application looks like from your description, but it's clear that you missed at least one important thing: for a component to know about changes to the store, it must be wrapped in an observer.
I reproduced approximately your application and here is a link where you can see how it should be implemented:
Codesandbox link >
嗯,听起来很奇怪。您可以添加此代码并告诉我它是否有效?
并将您的JSX更改为此:
Mmmh sounds strange. Can you add this code and tell me if it works ?
And change your JSX to this: