Skip to content

Commit f2c8e36

Browse files
committed
add documentation around rendering
1 parent 22af192 commit f2c8e36

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

readme.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
4. [Components With Children](#components-with-children)
1414
5. [Higher Order Components (HOC)](#higher-order-components-hoc)
1515
6. [Extending HTML Attributes](#extending-html-attributes)
16-
7. [Custom Elements / Web Components](#custom-elements--web-components)
16+
7. [Rendering](#rendering)
17+
8. [Custom Elements / Web Components](#custom-elements--web-components)
1718

1819
## Setup
1920

@@ -310,6 +311,27 @@ export default class ComponentWithHtmlAttributes extends Component<JSX.HtmlAttri
310311
<ComponentWithHtmlAttributes foo="bar" /> // throws error, invalid html attribute
311312
```
312313

314+
## Rendering
315+
316+
Rendering components requires an `Element`. The second argument will append your component to your node:
317+
318+
```jsx
319+
import { h, render } from 'preact';
320+
import MyComponent from './MyComponent';
321+
322+
render(<MyComponent />, document.body as Element);
323+
```
324+
325+
Additionally, you can choose to replace a specific node by specifying a third argument:
326+
327+
```jsx
328+
import { h, render } from 'preact';
329+
import MyComponent from './MyComponent';
330+
331+
const node: Element = document.getElementById('root') as Element;
332+
render(<MyComponent />, node, node.firstElementChild as Element);
333+
```
334+
313335
## Custom Elements / Web Components
314336

315337
With vanilla JSX in preact, you can create any element with whatever name you want and it'll get transpiled to `h('my-element-name', ...)`. However Typescript is more opinionated and will complain if an imported component or HTML element does not exist with that name. Normally, this is what you would want with Preact components but custom elements may or may not be imported into your JSX files.

0 commit comments

Comments
 (0)