Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set properties of undefined (setting 'toggle') #1051

Open
greeeg opened this issue Jan 23, 2024 · 16 comments
Open

Cannot set properties of undefined (setting 'toggle') #1051

greeeg opened this issue Jan 23, 2024 · 16 comments

Comments

@greeeg
Copy link

greeeg commented Jan 23, 2024

Do you want to request a feature or report a bug?
A bug

What is the current behavior?
Hi 👋, I'm getting new Sentry issues on my project since migrating from 10.0.1 to 10.0.3:

TypeError Object.setToggle
Cannot set properties of undefined (setting 'toggle')

I suspect the changes were introduced in 83988f1

Sorry I don't have more context since the stack trace does not include which line of your package throws the type error

@fkhadra
Copy link
Owner

fkhadra commented Jan 28, 2024

I'll try to reproduce the issue locally. More info that you could besides the error? Thank you

@greeeg
Copy link
Author

greeeg commented Jan 29, 2024

@fkhadra Not much since the code is minified in prod & I cannot reproduce it either locally, I only got:

TypeError: undefined is not an object (evaluating 'l.get(p).toggle=E')

I suspect it's coming from this exact line:

setToggle: (id: Id, fn: (v: boolean) => void) =>

@isalex78
Copy link

I get this error or attempt to have multiple toasts with different ids. Like push toast with id1, while it's still active - push another with id2 and again toast with id1. The last toast is not closing and the error thrown.

@Alfredoeb9
Copy link

Alfredoeb9 commented Feb 16, 2024

I had the similar issue looks like adding a containerId to your ToastContainer fixed the issue for me, and you can do a little overkill and check if that certain toastId for given ToastContainer ContainerId is active or not .

ex.

if (!toast.isActive(13, "friendRequest")) {
            console.log("first time running")
            toast('User does not exist', {
                position: "bottom-right",
                autoClose: false,
                closeOnClick: true,
                draggable: false,
                type: "error",
                toastId: 13                      
            })
 }

<ToastContainer containerId={"friendRequest"} />

@Pranay-Pandey
Copy link

Seems like the issue is because more than one ToastContainer are in the current reference. Fixed it by only having one ToastContainer in a parent component - Toast are created from many children components, but because all of them have only one instance of ToastContainer available, there is no issue.

The other method is to specify containerId for specific part where ToastContainer is present, I think

@arpitt45
Copy link

So, when you're using React Toastify, sometimes you might encounter this error that says something about having more than one ToastContainer. Basically, ToastContainer is like a box where all your toast notifications pop up on the screen. If you have more than one of these boxes showing up in your app, things can get a bit confused.

But no worries, there are two ways to fix it:

1.Just Stick to One Box (ToastContainer): The easiest way is to make sure you only have one ToastContainer in your whole app. You can think of it like having one central spot for all your toast messages to show up. Even if different parts of your app want to show a toast, they'll all use this same box, so there's no confusion.

Example: Imagine you have a big container for your whole app, and you just put the ToastContainer inside it. So no matter where a toast comes from, it always goes into that one container.

import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

function App() {
    return (
        <div>
            {/* Other components */}
            <div>
                {/* Component A */}
                <ToastContainer containerId="containerA" />
            </div>
            <div>
                {/* Component B */}
                <ToastContainer containerId="containerB" />
            </div>
        </div>
    );
}

export default App;

2.Give Each Part of Your App Its Own Box (ToastContainer): Now, if for some reason, you really need different parts of your app to have their own separate toast boxes, you can do that too. But you have to be smart about it. You give each box a unique "ID" using something called a "containerId". This way, even if there are multiple boxes, they each have their own identity, so they won't get mixed up.

Example: Let's say you have two sections in your app, and you want each section to have its own toast box. You'd create a ToastContainer for each section and give them different IDs, so they stay separate and organized.

import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

function SectionA() {
    const notify = () => toast.info("Toast from Section A");
    
    return (
        <div>
            <button onClick={notify}>Show Toast in Section A</button>
            <ToastContainer containerId="containerA" />
        </div>
    );
}

function SectionB() {
    const notify = () => toast.info("Toast from Section B");
    
    return (
        <div>
            <button onClick={notify}>Show Toast in Section B</button>
            <ToastContainer containerId="containerB" />
        </div>
    );
}

function App() {
    return (
        <div>
            <SectionA />
            <SectionB />
        </div>
    );
}

export default App;

@SunZhiC
Copy link

SunZhiC commented Mar 26, 2024

Seems like the issue is because more than one ToastContainer are in the current reference. Fixed it by only having one ToastContainer in a parent component - Toast are created from many children components, but because all of them have only one instance of ToastContainer available, there is no issue.

The other method is to specify containerId for specific part where ToastContainer is present, I think

It works, having one ToastContainer in a parent component.

@medAzizRezgui
Copy link

Adding limit={1} to the <ToastContainer/> fixed it for me.

@kathanshah1206
Copy link

So, when you're using React Toastify, sometimes you might encounter this error that says something about having more than one ToastContainer. Basically, ToastContainer is like a box where all your toast notifications pop up on the screen. If you have more than one of these boxes showing up in your app, things can get a bit confused.

But no worries, there are two ways to fix it:

1.Just Stick to One Box (ToastContainer): The easiest way is to make sure you only have one ToastContainer in your whole app. You can think of it like having one central spot for all your toast messages to show up. Even if different parts of your app want to show a toast, they'll all use this same box, so there's no confusion.

Example: Imagine you have a big container for your whole app, and you just put the ToastContainer inside it. So no matter where a toast comes from, it always goes into that one container.

import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

function App() {
    return (
        <div>
            {/* Other components */}
            <div>
                {/* Component A */}
                <ToastContainer containerId="containerA" />
            </div>
            <div>
                {/* Component B */}
                <ToastContainer containerId="containerB" />
            </div>
        </div>
    );
}

export default App;

2.Give Each Part of Your App Its Own Box (ToastContainer): Now, if for some reason, you really need different parts of your app to have their own separate toast boxes, you can do that too. But you have to be smart about it. You give each box a unique "ID" using something called a "containerId". This way, even if there are multiple boxes, they each have their own identity, so they won't get mixed up.

Example: Let's say you have two sections in your app, and you want each section to have its own toast box. You'd create a ToastContainer for each section and give them different IDs, so they stay separate and organized.

import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

function SectionA() {
    const notify = () => toast.info("Toast from Section A");
    
    return (
        <div>
            <button onClick={notify}>Show Toast in Section A</button>
            <ToastContainer containerId="containerA" />
        </div>
    );
}

function SectionB() {
    const notify = () => toast.info("Toast from Section B");
    
    return (
        <div>
            <button onClick={notify}>Show Toast in Section B</button>
            <ToastContainer containerId="containerB" />
        </div>
    );
}

function App() {
    return (
        <div>
            <SectionA />
            <SectionB />
        </div>
    );
}

export default App;

not at all correct

@wasiflatifhussain
Copy link

import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

You can simply only add one ToastContainer in your App.js and comment out or remove individual ToastContainers declared in each js file. Having many ToastContainers is causing the issue;

Simply having one ToastContainer in the App.js solved the issue through the web-app for me.

@kathanshah1206
Copy link

kathanshah1206 commented May 3, 2024 via email

@Stephenson30
Copy link

thank you worked for me

@ani1609
Copy link

ani1609 commented May 18, 2024

This error comes when you try to render more than one in a single layout. Like in my case, i was having a next app and i was trying to render this in many components which ended up with the above error. Instead, what you can do is, just move the to the parent renderer like layout.tsx in my case. This will resolve the issue.

@Stephenson30
Copy link

Stephenson30 commented May 18, 2024 via email

@luc345
Copy link

luc345 commented Jun 5, 2024

After updating react-toastify to version 10.0.5 in the same Next.js codebase, I encountered the same issue. The toast appears successfully at first but cannot be closed, and then it crashes with the error 'TypeError: Cannot set properties of undefined (setting 'toggle')'.

@kathanshah1206
Copy link

kathanshah1206 commented Jun 5, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests