GithubHelp home page GithubHelp logo

Comments (4)

Sumit-Kumar-0 avatar Sumit-Kumar-0 commented on June 15, 2024

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.

julia-fix avatar julia-fix commented on June 15, 2024

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.

mekanhaji avatar mekanhaji commented on June 15, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.