Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to override notification configuration for specific notification only?

I use react-toastify for notifications in my React app. In its configuration I’ve set a default autoClose setting. Is there a way that I can override this setting for specific notifications only? Please find my current code below.


I set react-toastify default configuration in the index page:

import { ToastContainer } from "react-toastify";
<ToastContainer
    closeButton={false}
    autoClose={6000}
/>

On other pages I for example have the code blow. It’s there where I would like to set a custom configuration for autoClose for that specific message only.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Notify({
        message: `A message`,
    });

This uses a component called Notify:

const User = ({ message, closeToast }) => (
    <div key={0} className="notification">
        <span style={{ backgroundImage: `url('/icons/user.svg')` }}></span>
        <label dangerouslySetInnerHTML={{ __html: message }}></label>
        <button onClick={closeToast}>+</button>
    </div>
);

const Notification = (props) => {
    const { message, user } = props;
    return (
        <div>
            toast(<User message={message} />, {
                className: "white-background",
                bodyClassName: "grow-font-size",
                progressClassName: "fancy-progress-bar",
            })
        </div>
    );
};

>Solution :

According to the documentation for react-toastify, you can pass the autoClose prop into the toast() emitter, as well as the ToastContainer

toast('My message', {autoClose: 5000});

https://fkhadra.github.io/react-toastify/introduction/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading