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

[Question] Dynamic values and class repetition #1588

Open
juanbiberretta opened this issue Nov 29, 2021 · 4 comments
Open

[Question] Dynamic values and class repetition #1588

juanbiberretta opened this issue Nov 29, 2021 · 4 comments

Comments

@juanbiberretta
Copy link

juanbiberretta commented Nov 29, 2021

Hello!

I was hoping to clear up something that seems like a bug to me. I was under the impression, that when you had a, say, React component with dynamic values as the button in the docs example: https://cssinjs.org/react-jss/?v=v10.8.2#dynamic-values, that it generated a classname that other instances of the same button with the same dynamic values would reuse.

What I mean by that is that if I have multiple components that use the default prop values, since the css values at the end of the day are the same, it'll just reuse the classname and not generate another one.

If we take the following example based off of the Dynamic Values docs:

import * as React from "react";
import { createUseStyles } from "react-jss";

const useStyles = createUseStyles({
  myButton: {
    padding: (props) => props.spacing,
  },
});

const Button = ({ children, ...props }) => {
  const classes = useStyles(props);

  return <button className={classes.myButton} />;
};

Button.defaultProps = {
  spacing: 10,
};

const IndexPage = () => {
  return (
    <>
      <Button />
      <Button />
      <Button />
    </>
  );
};

export default IndexPage;

and take a look at the generated styles:

.myButton-0-2-1 {}
.myButton-d0-0-2-2 {
  padding: 10px;
}
.myButton-d1-0-2-3 {
  padding: 10px;
}
.myButton-d2-0-2-4 {
  padding: 10px;
}

I fail to understand why myButton-d0-0-2-2, myButton-d1-0-2-3 and myButton-d2-0-2-4 are all separated out, as well as why .myButton-0-2-1 is empty?

Am I misunderstanding some core tenant of JSS? How can I reuse the same classname across all my components?

Thank you!

@aDarcy99
Copy link

aDarcy99 commented Dec 5, 2021

Have you found an answer to this yet?

@juanbiberretta
Copy link
Author

juanbiberretta commented Dec 6, 2021

Have you found an answer to this yet?

Nope :/ you dealing with something similar?

@aDarcy99
Copy link

aDarcy99 commented Dec 7, 2021

Yea noticed the same thing in one of my personal projects. I've switched to emotionJS for now, they have very similiar syntax if you use object styles. Main reason why I made the switch is it seems like theres a lot more community support and answers. Although you might not be in a situation where you can switch.

@seiyab
Copy link
Member

seiyab commented Dec 9, 2021

For .myButton-0-2-1, it is a class name for static rules.
In your example, it is empty because byButton doesn't have any static rules.
If you add a static rule, it will be appear in .myButton-0-2-1 .
https://codesandbox.io/s/goofy-forest-2z6g9?file=/src/App.tsx

For .myButton-d0-0-2-2, .myButton-d0-0-2-3, .myButton-d0-0-2-4, each of them is for each of Button.
If a Button have different spacing, it is applied independently.
Though some packages might reuse classes well comparing props, it is not straightforward.
As far as I know, JSS just have classes for each component if you write dynamic style.

And, for some situation, JSS might cause memory leak for another reason #1360 .
I hope recent PR resolved it partly, but it might still happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants