In react stateless functional components we'll commonly write something like this:
export function MyCompoment({
title,
foo,
bar
}) {
return <div> title: {title}, ...</div>
}
Where we're immediately destructuring the props object into its variables.
Now I'm using a Material-UI makeStyles
hook, and I'm also using TypeScript, and the way I'm currently using it looks like this.
const useStyles = makeStyles((theme : Theme ) =>({
root: ({foo} : MyComponentProps) => ({
content: foo
})
});
interface MyComponentProps {
title: string;
foo: string;
bar: string;
}
export function MyCompoment({
title,
foo,
bar
} : MyComponentProps) {
const classes = useStyles({
title,
foo,
bar
});
return <div> title: {title}, ...</div>
}
And you can see the problem - I have to repeat the props variable names to pass into the classes.
The best way I can think to avoid this, is write it this way:
export function MyCompoment(props: MyComponentProps) {
const {
title,
foo,
bar
} = props;
const classes = useStyles(props);
return <div> title: {title}, ...</div>
}
But that's a little messier than I was wanting.
I was wondering if it's possible to do something like:
export function MyCompoment({
title,
foo,
bar
} = props : MyComponentProps) {
const classes = useStyles(props);
return <div> title: {title}, ...</div>
}
Pretty picky I know, just wondering.