Comments (3)
Thanks so much for your thoughtful comments @pimterry.
I started out on this path as I noticed that the portal containers in some examples are managed through direct DOM intervention. So I thought, well, how about portals themselves.
I now think of portals as components with normal react attributes, except they don't appear in the DOM, just the virtual DOM. To make this directly manageable, I create a portal list (cache), and wrap that around individual portal wrappers, which contain the portals (InPortal).
const wrapperstyle = {display:'none'} // static
// hidden portal wrapper for clarity and usage of conventional react relisting services
export const PortalWrapper = ({
portal, index,
}) => {
return <div data-type='portalwrapper' data-index = {index} style = {wrapperstyle} key={index}>
{portal}
</div>
}
// portal list component for rapid relisting of updates, using external callback for set state
export const PortalList = ({scrollerID}) => {
const [portalList, setPortalList] = useState(null)
const isMounted = useIsMounted()
useEffect(()=>{
scrollerPortalCallbacks.set(scrollerID,
{setListState:()=>{
isMounted() && setPortalList(scrollerPortalListData.get(scrollerID).portalList)
}})
},[])
return portalList
}
Note that the portal list can be refreshed from outside (a portal manager), whenever the list is updated. This takes advantage of React's normal (fast!) refresh capability.
The portal list is embedded in a high level component (but hidden) to activate the portals.
return <div data-type = 'scroller' data-scrollerid = { scrollerSessionID }>
<div data-type = 'portalroot' style = { portalrootstyle }>
<PortalList scrollerID = { scrollerSessionID }/>
</div>
<Viewport
...
The OutPortal is pulled into its display shell like this:
portalchildRef.current = useMemo(()=>{
if (portalStatus != 'render') return portalchildRef.current
portallistitem.reparenting = true
let reverseportal = portallistitem.reverseportal
return <OutPortal node = {reverseportal} />
}, [portalStatus])
...
return <div ref = { shellRef } data-type = 'cellshell' data-scrollerid = {scrollerID} data-index = {index} data-instanceid = {instanceID} style = {styles}>
{ (portalStatus == 'render') && portalchildRef.current }
</div>
All this seems to be quite stable and performative. Note that I keep and pass around your portal metadata (reverseportal
).
Thanks so much for your tool Terry! It's brilliant, and a life-saver.
from react-reverse-portal.
Oh, and to be clear, the portal is destroyed when its wrapper is destroyed.
from react-reverse-portal.
As far as I know, there's no specific steps required.
First you'll create a portal node, and one or more InPortals and OutPortals and use them. Once you're done, the In/OutPortals will be unmounted by React (which cleans up their DOM state) and then they'll be garbage collected, and the portal node will be garbage collected as soon as your own code doesn't hold a reference to it anymore (which is necessarily after the various portals have been collected, so the DOM has been cleaned up by this stage).
React-Reverse-Portal doesn't track any persistent state outside those objects, so once they're not referenced as far as I'm aware they're gone.
I'm not super knowledgeable about how fibers reference portals internally, but as far as I know there's no need to get involved with the internals of that. There's no documented need to manually clean up portals, and any issue there sounds like a memory leak in React in general.
Is there a specific behaviour you're seeing where this doesn't happen? If you have a reproducible leak I can look at that would be very interesting.
from react-reverse-portal.
Related Issues (20)
- iframes reload from source HOT 6
- Race condition when swapping nodes between <OutPortal/>s HOT 5
- Any way to prevent unmounting of the portal contents when changing portals that aren't located within the same component? HOT 2
- Child components are re-rendered when switching their OutPortal HOT 1
- How to Create Multiple InPortals HOT 1
- Not rendering before an outPortal is used HOT 2
- Unhandled Rejection (NotFoundError): Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node. HOT 9
- Giving style to intermediary div HOT 3
- Migrate from Travis to Github Actions? HOT 1
- Rendering portal in a new window causes error: "html" portalNodes must be used with html elements, but OutPortal is within DIV HOT 1
- Nextjs error HOT 1
- Queries around library usage HOT 1
- Doesn't work when there is nested `portals.OutPoral` HOT 6
- moving OutPortal doesn't cause unmount/mount, but *does* cause re-render HOT 2
- Props stay the same when InPortal content is hidden HOT 3
- Text field autoFocus doesn't work in a portal HOT 1
- Scroll position doesn't maintain HOT 2
- Some non-hook function results are not preserved after conditional OutPortal HOT 2
- Support for Preact HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-reverse-portal.