-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unmountComponentAtNode compat rax (#6424)
* fix: fixup unmountComponentAtNode compat * fix(rax-compat): compat for rax driver-dom on nullish render container * fix(rax-compat): compat for rax driver-dom on non-specified render container
- Loading branch information
1 parent
154ab61
commit 32dfaac
Showing
5 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'rax-compat': patch | ||
--- | ||
|
||
fix: fix unmountComponentAtNode compat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { Root } from 'react-dom/client'; | ||
|
||
/** | ||
* A WeakMap that keeps track of container mouted root. | ||
* @key The container element passed to `render` function, which call `createRoot` and `root.render` internally. | ||
* @value The root instance returned by `createRoot` function. | ||
*/ | ||
export const containerRootMap = new WeakMap<Element | DocumentFragment, Root>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
import type { Container } from 'react-dom'; | ||
import { containerRootMap } from './container-root-map.js'; | ||
|
||
/** | ||
* Unmount a component rendered to a DOM node. | ||
* To unmount a component strictly, its state and event handlers should be cleaned up appropriately, | ||
* Which means we should let `React` do the unmounting work. | ||
* | ||
* In React 18, this method will be replaced by [root.unmount](https://react.dev/reference/react-dom/client/createRoot#root-unmount), | ||
* We preserve this method for Rax API compatibility. | ||
* | ||
* As we cannot approach the root instance from container element directly, | ||
* we use a WeakMap to keep track of container mouted root, | ||
* which saves the container-root relation in `render`(and `createRoot()` under the hood) execution. | ||
* @param container The container element which to be unmounted. | ||
*/ | ||
export default function unmountComponentAtNode(container: Container) { | ||
// @TODO: unmount | ||
container.textContent = ''; | ||
const relatedRoot = containerRootMap.get(container); | ||
// Ensure the root exists, then unmount root and remove it from containerRootMap. | ||
if (relatedRoot) { | ||
relatedRoot.unmount(); | ||
containerRootMap.delete(container); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters