Is it safe? Sure, but it probably won't work the way you expect.
First off, you can totally dump dom inside a
div that React created.
I've met several React programmers who would contest this fact, but Reacts design expects and accounts for editing dom directly when necessary. If they didn't, there would be no
componentDidUpdate or React
This documentation on integration with other libraries is probably the most relevant.
Here's the scoop:
- React won't touch the internals of a rendered div, assuming it's always empty at the end of render. It will only:
- Create new elements, which is irrelevant here as long as you leave it empty.
- Update existing elements that were originally created by react.
- Delete elements that were originally created by react.
So just create that empty div and leave it alone in
render and React won't mess with it.
Here's the rub; React guarantees nothing about the timing of the renders.
This means that you have no idea if the element will actually be there when
Pagination renders, causing the query to fail and the portal to not display.
componentDidUpdate only works because React specifically runs it after the dom is updated. But render is run before the dom is updated. So if
Pagination is rendered in the same sweep as
Grid that div is likely not mounted yet.
Now the infamous stack overflow Just-Don't-Do-That answer:
Just don't do that. The purpose of portal is to let you render outside the React container while keeping your components inside of Reacts render tree. If you're rendering inside React render tree anyways why don't you just render the components there? (if that statement is confusing I blame you)
The Portals doc mention that you can render a portal in a DOM node but nothing about components DOM
This isn't mentioned because this is special case of general rule; DOM shouldn't be accessed directly in React if there are more idiomatic ways.
A lot of memory leaks occur in DOM. In case a component that hosts a portal (
Grid) is re-rendered and portal component (
Pagination) is not, this will result in detached DOM, i.e. memory leak.
An element where a portal is attached may not even exist at the time when portal component renders.
- Is it safe to render a react portal into another component DOM?
- Use React Portal to render a child into a DOM node of the parent component
- Render react component into single DOM element
- React complains that hook is not used inside of a body function when my component is passed into another component to render
- How to render multiple component which parent are wrapped into another multiple element in react router v6
- How to pass in a react component into another react component to transclude the first component's content?
- Return DOM element in render function of React component
- How to render React Component into itself, in a recursive way
- Reactjs How to insert react component into string and then render
- How do you dynamically insert a React component into the DOM (for instance a dialog)?
- Generic React TypeScript component with 'as' prop (able to render any valid dom node)
- React Router Dom Link in another component
- Render multiple React components into a single DOM element
- How to access state of one component into another component in react
- React router dom redirect problem. Changes url, does not render component
- Dynamically insert react components into another react component
- Why does my React component remain in the DOM after navigating to another page?
- Render Same Component With Multiple Paths React Router Dom
- How to render react component inside function in another component?
- Dynamically render a component inside a another component in React
- React router dom changes url, does not render component
- how to render react component to another web app
- Cannot render Component passing React state to props for another Component
- add attributes to a react component passed into another component via props
- What's the best way to inject a React component into another React component? Or should I bother?
- Is it a good practice to use getter to render another component inside React component?
- Best Way to Render Multiple Paragraph Text read from JSON into React Component
- React passing data into another component
- Render a component inside another React
- Render HTML DOM Node in React Component
More Query from same tag
- How do I detect when I've reached the end of an array in a react functional component?
- VSCode autoimport not working after using absolute imports in NextJS
- Input Box problem In reactjs, On single click cursor is not pointing in text-box , On double click in the text-box Input Cursor is coming
- React Router Dom v6 with Typescript
- How to make image size dependent on the parents font size
- Failed prop type error but items are loaded
- Passing props to child using Route
- Get data using useeffect hook in react js
- React typescript, Property 'propTypes' does not exist on type 'typeof TodoFilterItem'. TS2339
- React router access params in TypeScript
- If the time changed from the clock, how the input will be change with the value of clock?
- Conditional onclick funtion firing without click?
- How to redirect to Login page when users not logged in the application
- Dynamically adding or removing items within a react-hook-forms form and registering the inputs?
- React component of grandparent callback doesnt re-render page after being call in grandchild
- How to pass a conditional operator through typescript boolean field?
- Difference between declarative and imperative in React.js?
- Pass data from API to another component with TypeScript and ReactJS
- Star Component Map method
- In the ToDo app when click on a date, I get a list of duplicate of task. How to fix it?
- how to convert functional component to class component?
- react parsing error inside functional component
- How to display one value from many inputs randomly with React hooks
- Change react jsx class dynamically without DOM manipulation
- how to fetch image from the API using react js
- Value is not updating on the input entry in JS in React app
- Next.js TypeError: Cannot read property '' of undefined when trying to console log api results
- reactjs dynamically adding a form input
- Buttons has no background on Safari iOS