简体   繁体   中英

React : How do I trigger function that change style inside the child component when hovering parent

Recently I made a component of the Item called ItemsPageItem inside of it I have a couple of other components and the one is called OnHoverAboutBlock should change some styles of inside located Grid . I made flag for the OnHoverAboutBlock which I am passing in props, looks like this:

export default function ItemsPageItemContent({serialNum, title, price, shortDescription, type}) {
const localStyles = useStyles();

const [isHover, setIsHover] = useState(false);

function onEnter(e){
    e.target.style.position = "absolute";
    e.target.style.overflow = "visible";
    e.target.style.height = 350;
}
function onLeave(e){
    e.target.style.position = "relative";
    e.target.style.overflow = "hidden";
    e.target.style.height = 135;
}

    return (
        <>
            <Grid container onMouseEnter={`${() => setIsHover(true)}`} onMouseLeave={`${() => setIsHover(false)}`} className={localStyles.itemBoxContainer}>
                <Grid item className={localStyles.itemBox} xs={12}>
                    <Grid item xs={12} className={localStyles.widgetBlock}>
                        <Grid item xs={2}>
                            <SerialNumber serialNumber={serialNum}/>
                        </Grid>
                        <Grid item className={localStyles.typeBox} xs={8}>
                            <TypeOfLot type={type}/>
                        </Grid>
                        <Grid item xs={2}>
                        </Grid>
                    </Grid>
                </Grid>
                    <OnHoverAboutBlock **isHover={isHover}** title={title} price={price}
                                       desc={shortDescription} type={type}/>
            </Grid>
        </>
    );

The question is how do I trigger the functions onEnter onLeave inside the child. I tried to write these functions inside the OnHoverAboutBlock but I still don't understand how do I trigger these functions. The code of OnHoverAboutBlock look like this:

export const OnHoverAboutBlock = ({title, price, desc, type, **isHover**}) => {
const localStyles = useStyles();

let buttonText = "Place Bid";

switch (type) {
    case "Product":
        buttonText = "Buy Now";
        break;
    case "Donation":
        buttonText = "Make Donation";
        break;
    default:
        break;
}

return (
    <>

        <Grid item className={`${localStyles.hoverAboutBlock}`} xs={12}>
            <Grid item className={localStyles.mainDesc} xs={12}>
                <Typography className={localStyles.secondaryTextColor} variant="h5" component="h2">
                    {title}
                </Typography>
                <Typography variant="subtitle2">
                    {price}
                </Typography>
            </Grid>
            <Grid item xs={12}>
                <Typography variant="body1" className={localStyles.bodyTextColor}>
                    {desc}
                </Typography>
            </Grid>
            <Grid item className={localStyles.bottomWidgetsBlock} xs={12}>
                <Grid item xs={8}>
                <Button className={localStyles.button}>
                    {buttonText}
                </Button>
                </Grid>
                <Grid item className={localStyles.likePlusBlock} xs={4}>
                    <Fab className={localStyles.bottomWidgetButtons}>
                        <AddIcon/>
                    </Fab>
                    <Fab>
                        <LikeIcon/>
                    </Fab>
                </Grid>
            </Grid>
        </Grid>
    </>
)

}

So, as well as I understand, you are trying to change the styles of the child component based on hovering over the parent. I see you already have created an isHover and you are setting it to true on mouse enter and false on mouse leave. Since you are already passing isHover as a prop to the child component, what you can do is use a condition inside your child component as follows

let customStyleClasses = "";
if(isHover){
  customStyleClasses = "styles-you-wanted-to-define-for-the-child-component"
}

Now you can add this customStyleClasses to your Grid component as follows:

export const OnHoverAboutBlock = ({title, price, desc, type, **isHover**}) => {
  .
  .
  .
  return (
    <>
      <Grid item className={`${localStyles.hoverAboutBlock} ${customStyleClasses }`} xs={12}>
    .
    .
    .
    .
    );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM