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

🧐 Using with class components #85

Open
dashqa opened this issue Jul 27, 2023 · 2 comments
Open

🧐 Using with class components #85

dashqa opened this issue Jul 27, 2023 · 2 comments

Comments

@dashqa
Copy link

dashqa commented Jul 27, 2023

Any chance of using this library with class components?
I tried to make HOC that passes useStyle hook properties to the class component, but now it's not possible to pass classname like className={styles.name} because antd-style generates random class name.

@dashqa dashqa changed the title 🧐[问题] 🧐 Using with class components Jul 27, 2023
@arvinxx
Copy link
Collaborator

arvinxx commented Sep 4, 2023

maybe I can add a method like withStyle HOC to support class Component ?

@dashqa
Copy link
Author

dashqa commented Sep 8, 2023

I think the HOC implementation would be helpful to other developers and is included in the antd-style package.

I have already implemented it as follows:

const withStyles = (WrappedComponent, useStyles) => {
  const WithProps = React.memo((props) => {
    return <WrappedComponent {...props} />;
  });

  function WithStyles(props) {
    const styleProps = useStyles(props);
    const themeModeProps = useThemeMode();
    const responsiveProps = useResponsive();

    const styles = useMemo(
      () => ({
        ...styleProps,
        theme: themeModeProps,
        responsive: responsiveProps,
      }),
      [styleProps, themeModeProps, responsiveProps]
    );

    return <WithProps {...props} _style={styles} />;
  }

  return WithStyles;
};

export default withStyles;

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

2 participants