无法执行内联样式的操作,例如:hover
. 您可以使用 JS 方法onMouseEnter
和onMouseLeave
:
const style = { fontSize: "180%",transition: 'font-size 0.5s' };
...
<FaFacebookSquare style={style} onMouseEnter={({target})=>target.style.fontSize= "180%"}
onMouseLeave={({target})=>target.style.fontSize= "100%"}/>
或者您可以将它们分成 Component<StyledIcon/>
然后useRef
,useEffect
然后useState
进行悬停。
const style = { fontSize: "180%",transition: 'font-size 0.5s' };
export function StyledIcon(props){
const [hover,setHover] = useState(false)
const iconRef = useRef(null)
useEffect(()=>{
if(hover){
iconRef.current.style.fontSize="200%"
}
else{
iconRef.current.style.fontSize="180%"
}
},[hover]) // changing fontSize whenever the hover state is updated
const handleIconType = ()=>{
switch(props.type){
case 'facebook':
{
return <FaFacebookSquare style={style} ref={iconRef} onMouseEnter={()=>{setHover(true)}} onMouseLeave={()=>{setHover(false)}}/>
}
...// cases for different type of icon
default:
return null
}
}
return(
<>
<FaFacebookSquare style={style} ref={iconRef} onMouseEnter={()=>{setHover(true)}} onMouseLeave={()=>{setHover(false)}}/>
</>
)
}
export const BottomBar = () => {
return (
<StyledBottomBar>
<StyledIcon type="facebook">
</StyledIcon>
<StyledIcon type="instagram">
</StyledIcon>
</StyledBottomBar>
);
};