Skip to content

Commit

Permalink
Merge pull request #551 from jpudysz/feature/explanation
Browse files Browse the repository at this point in the history
docs: explain useUnistyles subscriptions
  • Loading branch information
jpudysz authored Feb 3, 2025
2 parents acda721 + 3af1a2c commit 719e054
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion docs/src/content/docs/v3/references/use-unistyles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: useUnistyles
description: Learn about escape hatch in Unistyles 3.0
---

import { Card } from '@astrojs/starlight/components'
import { Card, Aside } from '@astrojs/starlight/components'
import Seo from '../../../../components/Seo.astro'

<Seo
Expand Down Expand Up @@ -45,6 +45,26 @@ const MyComponent = () => {
}
```

<Aside title="Subscriptions">
Unistyles will monitor your destructured props and re-render your component only when the desired value changes.
If you use `theme`, it will automatically subscribe to theme changes.
Destructuring `rt` won't create an automatic subscription, as this object contains multiple values that can change frequently during your app's lifecycle.
To create a subscription, you need to use a specific property on the `rt` object, such as `rt.colorScheme` or `rt.screen.width`.

```tsx title="Case 1 - theme only subscription"
// subscribe to theme changes, rt is not yet used
const { theme, rt } = useUnistyles()
```
vs
```tsx title="Case 2 - theme and insets subscription"
// subscribe to theme changes, and insets
const { theme, rt } = useUnistyles()

rt.insets // reading this value will automatically subscribe to insets changes
```

</Aside>

### Why isn't it recommended?

We encourage using `withUnistyles` instead because it ensures only a single component is re-rendered instead of multiple components or the entire app. If you use this hook in a root component, you lose all the benefits of ShadowTree updates and trigger full app re-renders on every change.
Expand Down

0 comments on commit 719e054

Please sign in to comment.