Skip to content

Commit e23662e

Browse files
committed
junk: WIP
1 parent 23da10d commit e23662e

1 file changed

Lines changed: 107 additions & 57 deletions

File tree

content/en/guide/v10/api-reference.md

Lines changed: 107 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,11 @@ This page serves as a quick overview over all exported functions.
1313

1414
---
1515

16-
## Component
16+
## preact
1717

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-
class MyComponent extends Component {
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.
3619

37-
class MyComponent extends Component {
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).
48-
49-
## render()
20+
### render()
5021

5122
`render(virtualDom, containerNode, [replaceNode])`
5223

@@ -112,7 +83,9 @@ render(
11283

11384
> ⚠️ 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`.
11485
115-
## hydrate()
86+
### hydrate()
87+
88+
`hydrate(virtualDom, containerNode)`
11689

11790
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).
11891

@@ -124,7 +97,7 @@ const Foo = () => <div>foo</div>;
12497
hydrate(<Foo />, document.getElementById('container'));
12598
```
12699

127-
## h() / createElement()
100+
### h() / createElement()
128101

129102
`h(type, props, ...children)`
130103

@@ -150,34 +123,40 @@ h('div', { id: 'foo' }, h('span', null, 'Hello!'));
150123
// <div id="foo"><span>Hello!</span></div>
151124
```
152125

153-
## toChildArray
126+
### Component
154127

155-
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.
156129

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.
158131

159-
```jsx
160-
import { toChildArray } from 'preact';
132+
```js
133+
import { Component } from 'preact';
161134

162-
function Foo(props) {
163-
const count = toChildArray(props.children).length;
164-
return <div>I have {count} children</div>;
135+
class MyComponent extends Component {
136+
// (see below)
165137
}
138+
```
166139

167-
// props.children is "bar"
168-
render(<Foo>bar</Foo>, container);
140+
#### Component.render(props, state)
169141

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+
class MyComponent extends Component {
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+
}
178155
```
179156

180-
## cloneElement
157+
To learn more about components and how they can be used, check out the [Components Documentation](/guide/v10/components).
158+
159+
### cloneElement
181160

182161
`cloneElement(virtualElement, props, ...children)`
183162

@@ -197,17 +176,23 @@ render(
197176
// <a href="/" target="_blank">home</a>
198177
```
199178

200-
## createContext
179+
### createContext
180+
181+
`createContext(initialState)`
182+
183+
Creates a new Context object which can be used to pass data through the component tree without passing down props through each level.
201184

202185
See the section in the [Context documentation](/guide/v10/context#createcontext).
203186

204-
## createRef
187+
### createRef
205188

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.
207192

208193
See the [References documentation](/guide/v10/refs#createref) for more details.
209194

210-
## Fragment
195+
### Fragment
211196

212197
A special kind of component that can have children, but is not rendered as a DOM element.
213198
Fragments make it possible to return multiple sibling children without needing to wrap them in a DOM container:
@@ -231,3 +216,68 @@ render(
231216
// <div>C</div>
232217
// </div>
233218
```
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+
function Foo(props) {
232+
const count = 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).
278+
279+
### Suspense
280+
281+
### lazy
282+
283+
## preact/debug

0 commit comments

Comments
 (0)