Comments (4)
To remove headless toasts without using toast.dismiss(), you can update the state of the toasts to remove the toast you want to dismiss. Since the toasts object is likely controlled by some state management system (such as React's state or a context), you can modify the state directly to remove the toast you want to dismiss.
export const PositionedToast = () => {
const { toasts, handlers } = useToaster();
const dismissToast = (id) => {
handlers.dismiss(id);
};
return (
<div
style={{
position: 'static',
}}
>
{toasts.map((toast) => (
<div
key={toast.id}
className={classNames(style.toast, style[toast.type], toast.alignBottom && style.alignBottom)}
style={{
transition: 'all 0.5s ease-out',
opacity: toast.visible ? 1 : 0,
top: toast.position?.top,
left: toast.position?.left,
bottom: toast.position?.bottom,
right: toast.position?.right,
}}
onClick={() => dismissToast(toast.id)}
{...toast.ariaProps}
>
{toast.message}
</div>
))}
</div>
);
};
Try this i thought it should work
from react-hot-toast.
No, handlers
does not have dismiss
property. I ended up storing toast id in a local state and not rendering a toast if its id is in that array:
'use client';
import { useToaster } from 'react-hot-toast/headless';
import style from './PositionedToast.module.scss';
import classNames from 'classnames';
import { useState } from 'react';
export const PositionedToast = () => {
const { toasts } = useToaster();
const [deletedToasts, setDeletedToasts] = useState<string[]>([]);
return (
<div
style={{
position: 'static',
}}
>
{toasts.map((toast) => {
const dismissToast = () => {
setDeletedToasts([...deletedToasts, toast.id]);
};
return deletedToasts.includes(toast.id) ? null : (
<div
key={toast.id}
className={classNames(style.toast, style[toast.type], toast.alignBottom && style.alignBottom)}
style={{
transition: 'all 0.5s ease-out',
opacity: toast.visible ? 1 : 0,
top: toast.position?.top,
left: toast.position?.left,
bottom: toast.position?.bottom,
right: toast.position?.right,
}}
onClick={dismissToast}
{...toast.ariaProps}
>
{toast.message}
</div>
);
})}
</div>
);
};
from react-hot-toast.
@julia-fix
As the handler object doesn't have the 'dismiss' method as of yet, you can manage it by shadowing the toast object.
Here is a example:
const useShadowToaster = () => {
const { toasts } = useToaster();
const [shadowToasts, setShadowToasts] = useState([]);
const [deletedToastsId, setDeletedToastsId] = useState([]);
useEffect(() => {
const newToasts = toasts.filter(toast => !deletedToastsId.includes(toast.id));
setShadowToasts(newToasts);
}, [toasts, deletedToastsId]);
const dismiss = (toastId) => {
setDeletedToastsId(prev => [...prev, toastId]);
setShadowToasts(prev => prev.filter(toast => toast.id !== toastId));
};
return {
toasts: shadowToasts,
handler: {
dismiss,
},
};
};
from react-hot-toast.
Related Issues (20)
- How can i pass dynamic variable to toast.promise loading? HOT 1
- Allow customizing slide up/down animation
- Allow customizing exit animation when user manually dismisses toast HOT 2
- toast.loading() not working with nextjs 14 HOT 3
- Pause on Hover does not work with Headless UI HOT 1
- Dismiss toast by matching toastIds with regex
- Custom output direction HOT 1
- Support loading with "mask"
- Element type Error when using with nextjs and redux-toolkit
- Accessibility
- (Suggestion) Enhancing Web Accessibility
- Add delay to toast
- Feature Requests for warning and max toast
- Toast persists after page navigation HOT 1
- Support popover api
- how to support center-center? HOT 2
- TypeError: Failed to set an indexed property [0] on 'CSSStyleDeclaration': Indexed property setter is not supported.
- Linear gradient background HOT 1
- onClick event on toast.error/toast.success Icon
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hot-toast.