Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 材质ui useStyles真的需要整个道具对象吗?_Javascript_Reactjs_React Hooks_Material Ui - Fatal编程技术网

Javascript 材质ui useStyles真的需要整个道具对象吗?

Javascript 材质ui useStyles真的需要整个道具对象吗?,javascript,reactjs,react-hooks,material-ui,Javascript,Reactjs,React Hooks,Material Ui,根据,如果我们希望我们的组件支持带有类的样式覆盖,我们应该将整个props对象传递给useStyles钩子: function Nested(props) { const classes = useStyles(props); const { someProp1, someProp2, ...rest } = props; return (...); } 这迫使我们在组件的主体中而不是在组件的签名中分解道具 经过一些简短的讨论,我发现所有useStyles真正需要支持classes

根据,如果我们希望我们的组件支持带有
类的样式覆盖
,我们应该将整个
props
对象传递给
useStyles
钩子:

function Nested(props) {
  const classes = useStyles(props);
  const { someProp1, someProp2, ...rest } = props;
  return (...);
}
这迫使我们在组件的主体中而不是在组件的签名中分解道具

经过一些简短的讨论,我发现所有
useStyles
真正需要支持
classes
功能的是用
classes
道具传递一个对象:

function Nested({ someProp1, someProp2, classes ...rest }) {
  const localClasses = useStyles({ classes });
  return (...);
}

虽然这看起来很好,但我想知道我是否遗漏了什么。我不确定这些文档是否过于防御性,或者试图保持它的简短,也许他们想避免我们重命名我们的本地类变量,还不如说:“传递一个包含
属性的对象…”之类的话。但是可能还有更多,我遗漏了一些关键的东西。

它看起来像是从
makeStyles
返回的
useStyles()
函数(可能是
props
)。我没有深入研究它,但乍一看,我没有看到任何期望,即您实际上正在传递任何特定的对象/属性

更多信息,请查看此处的源代码链接

从'@material ui/Styles/withStyles'导入{ClassNameMap,Styles,WithStylesOptions};
从“../DefaultTheme”导入{DefaultTheme};
导出默认函数makeStyles<
主题=默认主题,
Props扩展对象={},
ClassKey扩展string=string,
>(
风格:风格,
选项?:省略,
):道具的钥匙永远不会伸出
? // `makeStyles`其中传递的`styles`不依赖于道具
(道具?:任意)=>ClassNameMap
://`makeStyles`其中传递的`styles`依赖于道具
(道具:道具)=>ClassNameMap;

但是如果没有
道具,它就无法工作,我想知道在某些情况下它是否需要其他道具。
import { ClassNameMap, Styles, WithStylesOptions } from '@material-ui/styles/withStyles';
import { DefaultTheme } from '../defaultTheme';

export default function makeStyles<
  Theme = DefaultTheme,
  Props extends object = {},
  ClassKey extends string = string,
>(
  styles: Styles<Theme, Props, ClassKey>,
  options?: Omit<WithStylesOptions<Theme>, 'withTheme'>,
): keyof Props extends never
  ? // `makeStyles` where the passed `styles` do not depend on props
    (props?: any) => ClassNameMap<ClassKey>
  : // `makeStyles` where the passed `styles` do depend on props
    (props: Props) => ClassNameMap<ClassKey>;