栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Material UI中将变量发送给withStyles?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Material UI中将变量发送给withStyles?

这是使用新的钩子语法执行此操作的示例:

index.js

import React from "react";import ReactDOM from "react-dom";import StyledComponent from "./StyledComponent";const CustomComponent = ({ children, className }) => {  return (    <p className={className}>      Just showing passing in the component to use rather than automatically      using a div.      <br />      {children}    </p>  );};function App() {  return (    <div className="App">      <StyledComponent color="green">        Here's my content with green underline      </StyledComponent>      <StyledComponent        component={CustomComponent}        color="blue"        hoverColor="orange"      >        Here's my content with blue underline that changes to orange on hover.      </StyledComponent>    </div>  );}const rootElement = document.getElementById("root");ReactDOM.render(<App />, rootElement);

StyledComponent.js

import React from "react";import { makeStyles } from "@material-ui/styles";const useStyles = makeStyles({  root: {    borderBottom: ({ color }) => `2px solid ${color}`,    "&:hover": {      borderBottom: ({ color, hoverColor }) => {        const borderColor = hoverColor ? hoverColor : color;        return `2px solid ${borderColor}`;      }    }  }});const StyledComponent = ({  component: ComponentProp = "div",  children,  color,  hoverColor}) => {  const classes = useStyles({ color, hoverColor });  return <ComponentProp className={classes.root}>{children}</ComponentProp>;};export default StyledComponent;

如果您愿意,可以将此

useStyles
方法放在自己的文件中,然后将其用作自定义钩子,以使它生成的类(仍然具有变量支持)可用于多个组件(而不仅仅是
StyledComponent
)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号