andywer / andywer/react-usestyles

Allow eager styles preparation

未关闭
#1 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement help wanted
主要语言
JavaScript
星标
86
派生
2
PR 合并指标
30 天内没有已合并 PR

描述

There are [some opt-in options available](https://github.com/andywer/react-usestyles/blob/master/packages/style-hook/README.md#dynamic-styles--themes) to improve the performance, but there is one thing they cannot improve:

Every styles object has to be prepared initially and that happens each component mount. This preparation primarily means separation between static and dynamic style rules/declarations (function values).

Especially for components that get instantiated a lot that might have a serious performance impact.

## Possible solution

Could pass a reference to the component to `useStyles()`, so the styles can be prepared only once for each component instead of once for each component instance.

```jsx
import { useStyles } from "@andywer/style-hook"
import React from "react"

function Button () {
const classNames = useStyles({
default: {
background: theme => props.background || theme.button.background.default
// ...
}
}, Button, [props.background])
return // ...
}
```

Additional benefit:

If we have the component reference, we have access to the component's name. Could use that for a better development / debugging experience (i.e. put component name on style tags).

Can also stop `JSON.stringify()`-ing the styles for deduplication when having a component reference. Will improve the overall performance even more!

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。