1

I am attempting to render a component generated by a route; however, I need the component rendered to a specific element:

https://codesandbox.io/s/bold-feather-yh1bt

Clicking on the "Log It In" link on the home component will route you to the "/Login" component. In this component - clicking on any of the "Window" links renders the component correctly; however, I need the component rendered to the "logincontainer" elementID. How do I pull that off? Thanks All!

2
  • can you explain a bit more what is expected? Commented Aug 28, 2019 at 19:20
  • When I click on a Window link, I need the associated component rendered inside the "logincontainer" element. Commented Aug 28, 2019 at 19:21

2 Answers 2

2

You need to move

 <Route path="/Login/Win1" component={win1} />
 <Route path="/Login/Win2" component={win2} />
 <Route path="/Login/Win3" component={win3} />

Inside the logincontainer div.

like this

  <div
      id="logincontainer"
      style={{ backgroundColor: "Aqua", width: "100%", height: "200px" }}
    >
      <Route path="/Login/Win1" component={win1} />
      <Route path="/Login/Win2" component={win2} />
      <Route path="/Login/Win3" component={win3} />
  </div>

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need wrap them in a <Router>?
No, see my update. (the <Route> needs to be inside a <Router> but not directly so it is good because of the parent is wrapped inside a <Router>)
2

You can use react portals for that. More info here

render() {
  // React does *not* create a new div. It renders the component into `domNode`.
  // `domNode` is any valid DOM node, regardless of its location in the DOM.
  return ReactDOM.createPortal(
    <MyComponent />,
    domNode
  );
}

Comments

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.