-
-
Notifications
You must be signed in to change notification settings - Fork 521
VDOM properties documentation #1235
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
Open
natevw
wants to merge
12
commits into
preactjs:master
Choose a base branch
from
natevw:nvw/2025-02-vdom_ref
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
af6df16
stub in new VDOM props page w/link from sidebar
natevw ed853aa
rough first pass re. the `ref` property
natevw ca7af7c
add in rough notes re. `key` property
natevw 7847a68
first pass on style and class props
natevw b6243c7
go with some gory details for a bit
natevw 83a2abe
some event handling notes
natevw 5187be0
few tweaks to UTSL section
natevw 3d1f3d3
Update content/en/guide/v10/vdom-reference.md
natevw cf5248a
Update content/en/guide/v10/vdom-reference.md
natevw c15f280
Update content/en/guide/v10/vdom-reference.md
natevw e365a2a
drop a few more obscure details re. class/className handling
natevw 4c42736
add __html example
natevw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,96 @@ | ||
--- | ||
name: VDOM Properties Reference | ||
description: 'Learn more about how Preact uses special properties of your VDOM components' | ||
--- | ||
|
||
# VDOM Properties Reference | ||
|
||
This page describes how Preact applies rendered component properties to the DOM. | ||
|
||
--- | ||
|
||
<div><toc></toc></div> | ||
|
||
--- | ||
|
||
## key | ||
|
||
The value of this property provides an identity for the VDOM node when diffing against a previous render. | ||
See the [Keys tutorial](/tutorial/08-keys/) for background. | ||
|
||
Note that this is a particularly special property in that it is intercepted by Preact and *not* passed along to any custom component. | ||
|
||
|
||
## ref | ||
|
||
See the [References documentation](/guide/v10/refs#createref) for an introduction to this property. | ||
|
||
The value can be a function, in which case it will be called with a reference to the element (or component) once it is mounted. If the function returns a cleanup function, then it will be called when unmounting, otherwise the function will be called again with `null`. | ||
|
||
The value can also be a non-callable object, in which case Preact will assign the element (or null) to that object's `.current` property. | ||
|
||
Note that this is, like `key`, also a particularly special property in that this it is [not currently](https://github.com/preactjs/preact/pull/4658) passed through to custom components. See [forwardRef](/guide/v10/switching-to-preact#forwardref) for a workaround. | ||
|
||
|
||
## style | ||
|
||
This value can be a string containing inline CSS as it would appear in plain HTML. Note, however, that any string will be applied via `dom.style.cssText = value` which parses the CSS and will result in an empty attribute in case of any syntax error rather than the content being applied verbatim. | ||
|
||
This value can also be an object, in which case each of its individual entries are applied as [individual CSS properties](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty) on the element (at default [priority](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_cascade/Specificity#the_!important_exception)). Each key of your object may use the either the dashed style (e.g. `{"text-decoration-color": "blue"}`) or, for [CSS properties the browser is aware of](https://www.w3.org/TR/cssom-1/#dom-cssstyledeclaration-camel-cased-attribute), a camelCased style may be used as well (e.g. `{textDecorationColor: "blue"}`). | ||
|
||
### Numeric values | ||
|
||
Numeric values of style objects entries will have a "px" suffix **automatically appended** unless the CSS property name is either a custom one (e.g. `--my-var`) or is [known to be "non-dimensional"](https://github.com/preactjs/preact/blob/face9247724db0a74b764316c4486f384b89cfed/src/constants.js#L20-L21). This is for compatibility with React, but will likely be [moved into compat](https://github.com/preactjs/preact/issues/2621) in a future version. | ||
|
||
|
||
## class / className | ||
|
||
Preact core supports both `class` and `className` for setting the respective attribute/property. See [Preact's philosophy around raw attribute/property usage](/guide/v10/differences-to-react#raw-html-attributeproperty-names) for more discussion. | ||
|
||
|
||
|
||
## children | ||
|
||
|
||
The value of the property itself is under the control of Preact, e.g. in the following example despite passing some other value for the prop, `MyComponent` would not receive a `children` entry in its props because there are no actual child vnodes: | ||
|
||
```jsx | ||
|
||
<MyComponent children={[1,2,3]} /> | ||
``` | ||
|
||
## dangerouslySetInnerHTML | ||
|
||
For regular DOM nodes, this provides a way to take over the rendered contents. Like [React's version](https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html), it expects an object with an `__html` property whose value is to be set as the [innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) of the element: | ||
|
||
```jsx | ||
<div dangerouslySetInnerHTML={{ __html: '<h1>Hello World!</h1>' }} /> | ||
``` | ||
|
||
For functional/class-based components this is treated just as any other arbitrary property. The value would be available to the component's implementation but does not directly influence/override its rendered output. (TBD: but does [diff-skipping](https://preactjs.com/guide/v10/upgrade-guide/#dangerouslysetinnerhtml-will-skip-diffing-of-children) still apply?) | ||
|
||
## on* | ||
|
||
For property names beginning with "on…" Preact registers an event handler. See the [Events tutorial](/tutorial/02-events/) for additional explanation. | ||
|
||
Note that while Preact core does do a bit of fix up here and there (see [Other special properties](#other-special-properties) below…) it generally follows the underlying browser DOM event naming/behavior more plainly and directly when you are not using `preact/compat`. | ||
|
||
|
||
### on*Capture | ||
|
||
If your event property name ends with "…Capture" then Preact will register your event handler during the capture (rather than bubbling) phase. | ||
|
||
|
||
## Other special properties | ||
|
||
Preact core includes many other special property fixups including for: | ||
|
||
* certain form/display elements | ||
* certain event names and HTML attributes | ||
* various idiosyncracies of the [SVG DOM](https://www.w3.org/TR/SVG11/svgdom.html) | ||
* … etc. | ||
|
||
The list above is not intended to be comprehensive. | ||
|
||
There are many nuances both for specific bugfixes/workarounds/compatibility concessions, and other nuances that simply fall out of the how the internals have gotten implemented. | ||
Your best guide for advanced details would be to refer to the [`diff/props.js` source code](https://github.com/preactjs/preact/blob/main/src/diff/props.js), as well as the overarching diffing algorithm itself ([e.g.](https://github.com/preactjs/preact/blob/face9247724db0a74b764316c4486f384b89cfed/src/diff/index.js#L554-L576)) corresponding to the specific version of Preact you are using. |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.