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

ReactDOM.createPortal - Target container is not a DOM element. #3268

Closed
joetidee opened this issue Mar 23, 2018 · 2 comments
Closed

ReactDOM.createPortal - Target container is not a DOM element. #3268

joetidee opened this issue Mar 23, 2018 · 2 comments

Comments

@joetidee
Copy link

joetidee commented Mar 23, 2018

I am using ReactDOM.createPortal to place a component into a location in the DOM. The error Target container is not a DOM element. is being thrown.

Steps to reproduce

this functional component will place a supplied component into the DOM location 'toast-container':
const Toast = ({ children }) => ReactDOM.createPortal(children, 'toast-container');

This Toast component is being included within a story:

storiesOf('Elements/Toast', module)
    .add('Test', () => (
        <InlineWrapper>
            <Toast>
                <ToastTemplate
                    id="test"
                    type='success'
                >
                Your details have been saved.
                </ToastTemplate>
            </Toast>
        </InlineWrapper>
    ));

I have defined the DOM location in the html file located at storybook/index.html and have also tried doing the same in 'storybook/iframe.html' :

<body style="margin: 0;">
    <div id="root"></div>
    <div id="toast-container"></div>
    <script src="static/bundle.js"></script>
</body>

Please specify which version of Storybook and optionally any affected addons that you're running

  • @storybook/react 3.2.3
  • @storybook/addon-options 3.2.4
@danielduan
Copy link
Member

danielduan commented Mar 23, 2018

The first argument (child) is any renderable React child, such as an element, string, or fragment. The second argument (container) is a DOM element.

https://reactjs.org/docs/portals.html

You passed in a string instead of a DOM element.

@lukaskoeller
Copy link

lukaskoeller commented Sep 10, 2020

I am having the same problem but a different cause.

Can you help with that @danielduan ? Thank you in advance!

🚫 Throws Error when I want to show the Modal right away (true) without the need to first change isVisible state through an onClick event.

Target container is not a DOM element.

const [isVisible, setIsVisible] = useState(true);

✅ Works perfectly. When I click the "Open Modal" Button, the Modal opens as expected

const [isVisible, setIsVisible] = useState(false);

.storybook/preview.js

addDecorator(story => {
  const theme = useTheme(baseTheme);

  return (
    <>
      <ThemeProvider {...theme}>
        <Global styles={ css(globalStyles) } />
        <div id="app-root">
          {story()}
        </div>
        <div id="modal-root"></div>
      </ThemeProvider>
    </>
  )
});

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

No branches or pull requests

3 participants