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

Attach the parentSelector to a React component reference #767

Open
CMCDragonkai opened this issue Jul 23, 2019 · 4 comments
Open

Attach the parentSelector to a React component reference #767

CMCDragonkai opened this issue Jul 23, 2019 · 4 comments
Labels
Projects

Comments

@CMCDragonkai
Copy link

The ReactModal component has a parentSelector that is meant to return an HTML node to attach to.

However I want to be able to create modal boxes that are related to a parent component or higher order component.

Is there a way to attach the modal box to a particular react component and somehow forward references from the parent component to the react modal component?

@CMCDragonkai
Copy link
Author

Ok so I've tried a couple different methods involving React references, but they all don't work.

const modalContainerRef = React.createRef();

return (
  <div>
    <ContainerComponent ref={modalContainerRef} />
    <ModalComponent parent={modalContainerRef.current} />
  </div>
);

But the above fails because I'm using stateless functional components, and I'm also using styled components. Then it tells me to use React.forwardedRef, but that doesn't seem to work with styled components, I end up with loads of type errors.

Even if I use the old ref style:

let modalContainer;
return (
  <div>
    <ContainerComponent ref={c => {modalContainer = c;}} />
    <ModalComponent parent={modalContainer} />
  </div>
);

Variations of this including the usage of modalContainer.current or () => modalContainer.current all fail because it eventually complains that refs cannot be passed to the stateless functional components.

It seems the only solution is to pass some sort of unique id into the component for it to attach to.

@diasbruno
Copy link
Collaborator

Hi @CMCDragonkai, can you make a working example?

@CMCDragonkai
Copy link
Author

Well I ended up with just using a unique id and using document.getElementById to attach it, couldn't find a way to use React references when I'm also using stateless functional components.

@diasbruno
Copy link
Collaborator

@CMCDragonkai Check out #750, it seem to have an example that can help.

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

No branches or pull requests

2 participants