Skip to content

Commit

Permalink
docs: add fragment note (#385)
Browse files Browse the repository at this point in the history
To add the React Fragment requirement note the Preact 'Render Optimiztion' content was copied with changes to code example and added note.  This code was tested and working the correctly with React.

https://codesandbox.io/s/preact-signals-in-react-test-mzfv8x\?file\=/src/LogCount.tsx
  • Loading branch information
christowiz committed Jul 19, 2023
1 parent 9d0cb67 commit 3c23713
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ function Counter() {
}
```

### Rendering optimizations

The React adapter ships with several optimizations it can apply out of the box to skip virtual-dom rendering entirely. If you pass a signal directly into JSX, it will bind directly to the DOM `Text` node that is created and update that whenever the signal changes.

```js
import { signal } from "@preact/signals-react";

const count = signal(0);

// Unoptimized: Will trigger the surrounding
// component to re-render
function Counter() {
return <p>Value: {count.value}</p>;
}

// Optimized: Will update the text node directly
function Counter() {
return (
<p>
<>Value: {count}</>
</p>
);
}
```

To opt into this optimization, simply pass the signal directly instead of accessing the `.value` property.

> **Note**
> The content is wrapped in a React Fragment due to React 18's newer, more strict children types.
## License

`MIT`, see the [LICENSE](../../LICENSE) file.

0 comments on commit 3c23713

Please sign in to comment.