2

I know about lazy loading a component like:

import React, { lazy } from "react";
const Search = lazy(() => import('./components/search/Search'));

I was wondering how to handle imports like this with lazy?

import { ToastContainer, toast } from 'react-toastify';

1 Answer 1

4

lazy expects a promise of { default: ... } object to be returned.

In case a module doesn't follow this convention, a component should be re-exported as default in intermediate module:

export { ToastContainer as default, toast } from 'react-toastify';

Or handled in lazy function:

lazy(async () => {
  const { ToastContainer } = await import('react-toastify');
  return { default: ToastContainer };
});
Sign up to request clarification or add additional context in comments.

3 Comments

how to import 'toast' then? I need to import both ToastContainer and toast.I tried this: lazy(async () => { const { ToastContainer, toast } = await import('react-toastify'); return { default: ToastContainer, toast }; }); but no luck, it looks as if toast is not imported
Where do you intend to use toast? The question doesn't contain relevant code. lazy results in lazy-loaded component.
got it, Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.