Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow eager styles preparation #1

Open
andywer opened this issue Nov 11, 2018 · 2 comments
Open

Allow eager styles preparation #1

andywer opened this issue Nov 11, 2018 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@andywer
Copy link
Owner

andywer commented Nov 11, 2018

There are some opt-in options available 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.

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!

@andywer
Copy link
Owner Author

andywer commented Nov 11, 2018

First idea (dismissed)

import { prepareStyles, useStyles } from "@andywer/style-hook"
import React from "react"

const buttonStyles = prepareStyles({
  // ...
})

function Button () {
  const classNames = useStyles(buttonStyles())
  return // ...
}

If we also need access to the component props:

import { prepareStyles, useStyles } from "@andywer/style-hook"
import React from "react"

const buttonStyles = prepareStyles({
  default: {
    background: (theme, props) => props.background || theme.button.background.default
  }
  // ...
})

function Button () {
  const classNames = useStyles(buttonStyles(props), [props.background])
  return // ...
}

@andywer andywer added enhancement New feature or request help wanted Extra attention is needed labels Nov 11, 2018
@andywer
Copy link
Owner Author

andywer commented Nov 12, 2018

Maybe we can even get around passing the component reference manually in the long term... reactjs/rfcs#68 (comment)

This was referenced Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant