reactjstypescriptjsscss-in-js

Dynamic CSS in typescript - How to access parameter passed to the `useStyles` method


I'm trying to access the state of my component in the useStyles method created using the react-jss package. The code seems to be correct for javascript but not for typescript according to what I found online.


import { createUseStyles } from 'react-jss';

const useStyles = createUseStyles({
  myClass: {
    height: `${height}px`, // <== squiggly line here since I don't know how to pass the parameters
  },
});

export function Collapse() {

  const [height, setHeight] = useState(18);
  const classes = useStyles(height);

  return (
    <div>...</div>
  );
}

How can I achieve this?


Solution

  • Using a function in my styles allows me to access the desired parameter.

    
    import { createUseStyles } from 'react-jss';
    
    const useStyles = createUseStyles({
      myClass: props => ({
        height: `${height}px`,
      }),
    });
    
    export function Collapse() {
    
      const [height, setHeight] = useState(18);
      const classes = useStyles({height: height});
    
      return (
        <div>...</div>
      );
    }