thiras
thiras

Reputation: 551

Passing props to the makeStyle in TS

How can I pass post.mainImage to backgroundImage style.

Here is my code;

import React from 'react';
import { Post } from '../interfaces';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';

type Props = {
  post: Post;
}

const useStyles = makeStyles<Theme, Props>((theme: Theme) =>
  createStyles({
    root: {
      maxWidth: '100%',
      backgroundImage: ({ post }) => post.mainImage
    },
    date: {
      margin: theme.spacing(1),
      marginLeft: theme.spacing(2)
    },
    heroimage: {
      maxWidth: '100%',
      height: 'auto',
      objectFit: 'cover'
    }
  })
);

export default function HeroPost({ post }: Props) {
  const classes = useStyles({ post });
  return (
    <Container className={classes.root}>
      <img alt={post.title} src={post.mainImage} className={classes.heroimage} />
    </Container>
  );
}

The code below has passed without a problem from the linter. But still cannot get the backgroundImage value on the front.

Upvotes: 12

Views: 13632

Answers (5)

anandshandilya
anandshandilya

Reputation: 31

You need to pass props with its type directly where you want to access it. This Should work fine.

type BgProps = {
  mainImage: string;
}

const useStyles = makeStyles<Theme, Props>((theme: Theme) =>
  createStyles({
    root: {
      maxWidth: '100%',
      backgroundImage: (props: BgProps) => props.mainImage
    },
    date: {
      margin: theme.spacing(1),
      marginLeft: theme.spacing(2)
    }
  })
);

Upvotes: 3

Hamza Khattabi
Hamza Khattabi

Reputation: 674

Try this using Typescript :

   const useStyles = makeStyles(theme => ({
           root: ({mainImage}: {mainImage: string}) => ({
                 ...
                 backgroundImage: mainImage
           })        
    })
        
    const component: React.FC => {
        const classes = useStyles({mainImage})
         return ...
    }

Upvotes: 0

Lionel Rowe
Lionel Rowe

Reputation: 5926

You can supply type variables to the call to makeStyles (note that the first one must be the theme type and the second the prop type):

type Props = {
  post: Post;
};

const useStyles = makeStyles<Theme, Props>(theme =>
  createStyles({
    root: {
      maxWidth: '100%',
      backgroundImage: ({ post }) => `url("${post.mainImage}")`
    },
    // ...
  })
);

export default function HeroPost({ post }: Props) {
  const classes = useStyles({ post });

  return (
    // ...
  );
}

Upvotes: 37

Hamza Khattabi
Hamza Khattabi

Reputation: 674

Try this: useStyles is a hook which can take a props in parameters and return the method useStyles.

const useStyles = (props: Props) => {
       const {post} = props;
    
       return makeStyles(theme => ({
           root: {
              maxWidth: '100%',
              backgroundImage: post.mainImage
           },
      }))
    } 

Upvotes: 4

Yatrix
Yatrix

Reputation: 13795

You access the props like this,

backgroundImage: props => props.backgroundImageProp

https://material-ui.com/styles/api/#returns-3

Upvotes: 0

Related Questions