You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -13,40 +13,11 @@ This page serves as a quick overview over all exported functions.
13
13
14
14
---
15
15
16
-
## Component
16
+
## preact
17
17
18
-
`Component` is a base class that can be extended to create stateful Preact components.
19
-
20
-
Rather than being instantiated directly, Components are managed by the renderer and created as-needed.
21
-
22
-
```js
23
-
import { Component } from'preact';
24
-
25
-
classMyComponentextendsComponent {
26
-
// (see below)
27
-
}
28
-
```
29
-
30
-
### Component.render(props, state)
31
-
32
-
All components must provide a `render()` function. The render function is passed the component's current props and state, and should return a Virtual DOM Element (typically a JSX "element"), an Array, or `null`.
33
-
34
-
```jsx
35
-
import { Component } from'preact';
18
+
The `preact` module provides only essential functionality, such as rendering components and creating VDOM elements. Additional utilities are provided by the various subpath exports, such as `preact/hooks`, `preact/compat`, `preact/debug`, etc.
36
19
37
-
classMyComponentextendsComponent {
38
-
render(props, state) {
39
-
// props is the same as this.props
40
-
// state is the same as this.state
41
-
42
-
return<h1>Hello, {props.name}!</h1>;
43
-
}
44
-
}
45
-
```
46
-
47
-
To learn more about components and how they can be used, check out the [Components Documentation](/guide/v10/components).
> ⚠️ The `replaceNode`-argument will be removed with Preact `v11`. It introduces too many edge cases and bugs which need to be accounted for in the rest of Preact's source code. If you still need this functionality, we recommend using [`preact-root-fragment`](/guide/v10/preact-root-fragment), a small helper library that provides similar functionality. It is compatible with both Preact `v10` and `v11`.
114
85
115
-
## hydrate()
86
+
### hydrate()
87
+
88
+
`hydrate(virtualDom, containerNode)`
116
89
117
90
If you've already pre-rendered or server-side-rendered your application to HTML, Preact can bypass most rendering work when loading in the browser. This can be enabled by switching from `render()` to `hydrate()`, which skips most diffing while still attaching event listeners and setting up your component tree. This works only when used in conjunction with pre-rendering or [Server-Side Rendering](/guide/v10/server-side-rendering).
This helper function converts a `props.children` value to a flattened Array regardless of its structure or nesting. If `props.children`is already an array, a copy is returned. This function is useful in cases where `props.children` may not be an array, which can happen with certain combinations of static and dynamic expressions in JSX.
128
+
`Component`is a base class that can be extended to create stateful Preact components.
156
129
157
-
For Virtual DOM Elements with a single child, `props.children` is a reference to the child. When there are multiple children, `props.children` is always an Array. The `toChildArray` helper provides a way to consistently handle all cases.
130
+
Rather than being instantiated directly, Components are managed by the renderer and created as-needed.
158
131
159
-
```jsx
160
-
import { toChildArray } from'preact';
132
+
```js
133
+
import { Component } from'preact';
161
134
162
-
functionFoo(props) {
163
-
constcount=toChildArray(props.children).length;
164
-
return<div>I have {count} children</div>;
135
+
classMyComponentextendsComponent {
136
+
// (see below)
165
137
}
138
+
```
166
139
167
-
// props.children is "bar"
168
-
render(<Foo>bar</Foo>, container);
140
+
#### Component.render(props, state)
169
141
170
-
// props.children is [<p>A</p>, <p>B</p>]
171
-
render(
172
-
<Foo>
173
-
<p>A</p>
174
-
<p>B</p>
175
-
</Foo>,
176
-
container
177
-
);
142
+
All components must provide a `render()` function. The render function is passed the component's current props and state, and should return a Virtual DOM Element (typically a JSX "element"), an Array, or `null`.
143
+
144
+
```jsx
145
+
import { Component } from'preact';
146
+
147
+
classMyComponentextendsComponent {
148
+
render(props, state) {
149
+
// props is the same as this.props
150
+
// state is the same as this.state
151
+
152
+
return<h1>Hello, {props.name}!</h1>;
153
+
}
154
+
}
178
155
```
179
156
180
-
## cloneElement
157
+
To learn more about components and how they can be used, check out the [Components Documentation](/guide/v10/components).
Creates a new Context object which can be used to pass data through the component tree without passing down props through each level.
201
184
202
185
See the section in the [Context documentation](/guide/v10/context#createcontext).
203
186
204
-
## createRef
187
+
###createRef
205
188
206
-
Provides a way to reference an element or component once it has been rendered.
189
+
`createRef(initialValue)`
190
+
191
+
Creates a new Ref object that acts as a stable, local value that will persist across renders. This can be used to store DOM references, component instances, or any arbitrary value.
207
192
208
193
See the [References documentation](/guide/v10/refs#createref) for more details.
209
194
210
-
## Fragment
195
+
###Fragment
211
196
212
197
A special kind of component that can have children, but is not rendered as a DOM element.
213
198
Fragments make it possible to return multiple sibling children without needing to wrap them in a DOM container:
@@ -231,3 +216,68 @@ render(
231
216
// <div>C</div>
232
217
// </div>
233
218
```
219
+
220
+
### toChildArray
221
+
222
+
`toChildArray(componentChildren)`
223
+
224
+
This helper function converts a `props.children` value to a flattened Array regardless of its structure or nesting. If `props.children` is already an array, a copy is returned. This function is useful in cases where `props.children` may not be an array, which can happen with certain combinations of static and dynamic expressions in JSX.
225
+
226
+
For Virtual DOM Elements with a single child, `props.children` is a reference to the child. When there are multiple children, `props.children` is always an Array. The `toChildArray` helper provides a way to consistently handle all cases.
227
+
228
+
```jsx
229
+
import { toChildArray } from'preact';
230
+
231
+
functionFoo(props) {
232
+
constcount=toChildArray(props.children).length;
233
+
return<div>I have {count} children</div>;
234
+
}
235
+
236
+
// props.children is "bar"
237
+
render(<Foo>bar</Foo>, container);
238
+
239
+
// props.children is [<p>A</p>, <p>B</p>]
240
+
render(
241
+
<Foo>
242
+
<p>A</p>
243
+
<p>B</p>
244
+
</Foo>,
245
+
container
246
+
);
247
+
```
248
+
249
+
### isValidElement
250
+
251
+
`isValidElement(virtualElement)`
252
+
253
+
Checks if the provided value is a valid Preact VNode.
254
+
255
+
### options
256
+
257
+
See the [Option Hooks](/guide/v10/options) documentation for more details.
258
+
259
+
## preact/hooks
260
+
261
+
See the [Hooks](/guide/v10/hooks) documentation for more details. Please note that the page includes a number of "Compat-specific hooks" that are not available from `preact/hooks`, only `preact/compat`.
262
+
263
+
## preact/compat
264
+
265
+
`preact/compat` is our compatibility layer that allows you to use Preact as a drop-in replacement for React. It provides all of the APIs of `preact` and `preact/hooks`, whilst also providing a few more to match the React API.
266
+
267
+
### Children
268
+
269
+
### createPortal
270
+
271
+
### memo
272
+
273
+
### forwardRef
274
+
275
+
### StrictMode
276
+
277
+
Preact does not implement `StrictMode` and so the `StrictMode` component is just an alias of [`Fragment`](#Fragment). You can pass children to it, but for all debugging checks and warnings, you should instead use [`preact/debug`](#preactdebug).
0 commit comments