React DOM Components·续
If you render a built-in browser HTML element with an is attribute, it will also be treated as a custom element.
Setting values on custom elements
Custom elements have two methods of passing data into them:
1) Attributes: Which are displayed in markup and can only be set to string values 2) Properties: Which are not displayed in markup and can be set to arbitrary JavaScript values
By default, React will pass values bound in JSX as attributes:
<my-element value="Hello, world!"></my-element>
Non-string JavaScript values passed to custom elements will be serialized by default:
// Will be passed as `"1,2,3"` as the output of `[1,2,3].toString()`
<my-element value={[1,2,3]}></my-element>
React will, however, recognize an custom element's property as one that it may pass arbitrary values to if the property name shows up on the class during construction:
```js src/index.js hidden import {MyElement} from './MyElement.js'; import { createRoot } from 'react-dom/client'; import {App} from "./App.js";
customElements.define('my-element', MyElement);
const root = createRoot(document.getElementById('root'))
root.render(
```js src/MyElement.js active
export class MyElement extends HTMLElement {
constructor() {
super();
// The value here will be overwritten by React
// when initialized as an element
this.value = undefined;
}
connectedCallback() {
this.innerHTML = this.value.join(", ");
}
}
```js src/App.js
export function App() {
return
#### Listening for events on custom elements
A common pattern when using custom elements is that they may dispatch [`CustomEvent`s](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) rather than accept a function to call when an event occur. You can listen for these events using an `on` prefix when binding to the event via JSX.
```js src/index.js hidden
import {MyElement} from './MyElement.js';
import { createRoot } from 'react-dom/client';
import {App} from "./App.js";
customElements.define('my-element', MyElement);
const root = createRoot(document.getElementById('root'))
root.render(<App />);
```javascript src/MyElement.js export class MyElement extends HTMLElement { constructor() { super(); this.test = undefined; this.emitEvent = this._emitEvent.bind(this); }
_emitEvent() { const event = new CustomEvent('speak', { detail: { message: 'Hello, world!', }, }); this.dispatchEvent(event); }
connectedCallback() { this.el = document.createElement('button'); this.el.innerText = 'Say hi'; this.el.addEventListener('click', this.emitEvent); this.appendChild(this.el); }
disconnectedCallback() { this.el.removeEventListener('click', this.emitEvent); } }
```jsx src/App.js active
export function App() {
return (
<my-element
onspeak={e => console.log(e.detail.message)}
></my-element>
)
}
[!NOTE]
Events are case-sensitive and support dashes (
-). Preserve the casing of the event and include all dashes when listening for custom element's events:
// Listens for `say-hi` events
<my-element onsay-hi={console.log}></my-element>
// Listens for `sayHi` events
<my-element onsayHi={console.log}></my-element>
>
All SVG components
React supports all built-in browser SVG components. This includes:
<a><animate><animateMotion><animateTransform><circle><clipPath><defs><desc><discard><ellipse><feBlend><feColorMatrix><feComponentTransfer><feComposite><feConvolveMatrix><feDiffuseLighting><feDisplacementMap><feDistantLight><feDropShadow><feFlood><feFuncA><feFuncB><feFuncG><feFuncR><feGaussianBlur><feImage><feMerge><feMergeNode><feMorphology><feOffset><fePointLight><feSpecularLighting><feSpotLight><feTile><feTurbulence><filter><foreignObject><g><hatch><hatchpath><image><line><linearGradient><marker><mask><metadata><mpath><path><pattern><polygon><polyline><radialGradient><rect><script><set><stop><style><svg><switch><symbol><text><textPath><title><tspan><use><view>
[!NOTE]
Similar to the DOM standard, React uses a
camelCaseconvention for prop names. For example, you'll writetabIndexinstead oftabindex. You can convert existing SVG to JSX with an online converter.Namespaced attributes also have to be written without the colon:
xlink:actuatebecomesxlinkActuate.xlink:arcrolebecomesxlinkArcrole.xlink:hrefbecomesxlinkHref.xlink:rolebecomesxlinkRole.xlink:showbecomesxlinkShow.xlink:titlebecomesxlinkTitle.xlink:typebecomesxlinkType.xml:basebecomesxmlBase.xml:langbecomesxmlLang.xml:spacebecomesxmlSpace.xmlns:xlinkbecomesxmlnsXlink.
