admin管理员组文章数量:1415137
My English may be bad. I am working on a small personal project using vite. I have a notification component for showing notifications to the user. Currently I am calling the notification componentin every component where I have to give notifications like below.
const [notification, setNotification] = useState(null);
return (
{notification && <Notification type={notification.type} message={notification.message} />}
)
I know this not efficient as I am rendering the notification component, everytime I need in every component.
Is there any other approach to implement this so I don't need to render notification everytime I need. I am using zustand for state management. Is there any way we can use state management in a way that when I set a state of Notification store the component should be displayed or not.
My English may be bad. I am working on a small personal project using vite. I have a notification component for showing notifications to the user. Currently I am calling the notification componentin every component where I have to give notifications like below.
const [notification, setNotification] = useState(null);
return (
{notification && <Notification type={notification.type} message={notification.message} />}
)
I know this not efficient as I am rendering the notification component, everytime I need in every component.
Is there any other approach to implement this so I don't need to render notification everytime I need. I am using zustand for state management. Is there any way we can use state management in a way that when I set a state of Notification store the component should be displayed or not.
Share Improve this question asked Feb 12 at 21:23 Harsha BanaHarsha Bana 233 bronze badges1 Answer
Reset to default 3Move your notification component to a parent component. This parent will be in charge of showing the notifications—think of it as the notification manager. Since you're using Zustand, you won't need to worry about prop drilling to pass data around. The parent component can update the Zustand store, and your notification component (wherever it's rendered) can listen for changes to that store.
本文标签: javascriptSTRUGGLING WITH NOTIFICATION COMPONENTStack Overflow
版权声明:本文标题:javascript - STRUGGLING WITH NOTIFICATION COMPONENT - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745201285a2647385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论