Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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
Reactjs 如何在react with typescript中向样式化材质ui道具类添加属性_Reactjs_Typescript_Material Ui - Fatal编程技术网

Reactjs 如何在react with typescript中向样式化材质ui道具类添加属性

Reactjs 如何在react with typescript中向样式化材质ui道具类添加属性,reactjs,typescript,material-ui,Reactjs,Typescript,Material Ui,我想使用材料界面,使用反应和打字脚本。我想将属性传递给组件,但不知道如何传递。我正在使用MaterialUI repo中的react-typescript示例。我的尝试如下: 添加属性界面: const styles = (theme: Theme) => createStyles({ root: { textAlign: 'center', paddingTop: theme.spacing.unit * 20, }, }); inter

我想使用材料界面,使用反应和打字脚本。我想将属性传递给组件,但不知道如何传递。我正在使用MaterialUI repo中的react-typescript示例。我的尝试如下:

添加属性界面:

const styles = (theme: Theme) =>
  createStyles({
    root: {
      textAlign: 'center',
      paddingTop: theme.spacing.unit * 20,
    },
  });

interface IndexProps
  extends WithStyles<typeof styles> {
  myText: string;
}
我做错了什么?任何帮助都将不胜感激

为了完整起见,我的
packages.json
文件是:

{
  "name": "create-react-app-with-typescript",
  "version": "0.1.0",
  "private": true,
  "main": "src/index.tsx",
  "dependencies": {
    "@material-ui/core": "latest",
    "@types/react": "*",
    "@types/react-dom": "*",
    "react": "latest",
    "react-dom": "latest",
    "react-scripts-ts": "latest"
  },
  "devDependencies": {
    "@types/jest": "*",
    "@types/node": "*",
    "typescript": "latest"
  },
  "scripts": {
    "start": "react-scripts-ts start",
    "build": "react-scripts-ts build",
    "test": "react-scripts-ts test --env=jsdom",
    "eject": "react-scripts-ts eject"
  }
}

我假设你正在使用。如果将鼠标悬停在使用root调用
,您将看到其返回类型为:

(props: object) => JSX.Element
这是错误的:道具类型应该是
{myText:string}
,而不是
对象
。如果使用root查看
的定义,您将看到它的来源:

function withRoot(Component: React.ComponentType) {
  function WithRoot(props: object) {
这些行应替换为:

function withRoot<P>(Component: React.ComponentType<P>) {
  function WithRoot(props: P) {
参考
src/components/ItemsList.tsx
底部的定义:

export default withStyles(styles)<{}>(connect(mapStateToProps)(withWidth()(ItemsList))
);
type PropsOf<C> = C extends React.ComponentType<infer P> ? P : never;
const ItemsListWrapped = withStyles(styles)(connect(mapStateToProps)(withWidth()(ItemsList));
type FinalProps = PropsOf<typeof ItemsListWrapped>;
let x: FinalProps;
x.  // look at completion menu here (Ctrl-Space in Visual Studio Code)
不幸的是,这些复杂的类型操作导致类型表达式难以理解,甚至在悬停弹出窗口中被截断,因此我发现查看所有道具的最佳方法是查看完成菜单,如图所示。然后我们看到四个意想不到的道具:
历史
位置
匹配
,以及
静态上下文
。如果我们看一下这些可能来自的所有地方,希望它们可以识别为来自
ItemsList.Props extends RouteComponentProps
。事实上,该声明是错误的,因为您没有使用
ItemsList
作为路由组件,也没有手动传递所有这些道具,
ItemsList
也没有实际使用任何道具,除了
历史
,您再次声明(并且确实传递了)。因此,只需从扩展列表中删除
RouteComponentProps


正如我们所看到的,使用包装的React组件时的类型错误可能有许多不同的原因。我希望我已经给了您一些关于如何自己追踪未来此类错误的知识。

我假设您正在使用。如果将鼠标悬停在使用root调用
,您将看到其返回类型为:

(props: object) => JSX.Element
这是错误的:道具类型应该是
{myText:string}
,而不是
对象
。如果使用root查看
的定义,您将看到它的来源:

function withRoot(Component: React.ComponentType) {
  function WithRoot(props: object) {
这些行应替换为:

function withRoot<P>(Component: React.ComponentType<P>) {
  function WithRoot(props: P) {
参考
src/components/ItemsList.tsx
底部的定义:

export default withStyles(styles)<{}>(connect(mapStateToProps)(withWidth()(ItemsList))
);
type PropsOf<C> = C extends React.ComponentType<infer P> ? P : never;
const ItemsListWrapped = withStyles(styles)(connect(mapStateToProps)(withWidth()(ItemsList));
type FinalProps = PropsOf<typeof ItemsListWrapped>;
let x: FinalProps;
x.  // look at completion menu here (Ctrl-Space in Visual Studio Code)
不幸的是,这些复杂的类型操作导致类型表达式难以理解,甚至在悬停弹出窗口中被截断,因此我发现查看所有道具的最佳方法是查看完成菜单,如图所示。然后我们看到四个意想不到的道具:
历史
位置
匹配
,以及
静态上下文
。如果我们看一下这些可能来自的所有地方,希望它们可以识别为来自
ItemsList.Props extends RouteComponentProps
。事实上,该声明是错误的,因为您没有使用
ItemsList
作为路由组件,也没有手动传递所有这些道具,
ItemsList
也没有实际使用任何道具,除了
历史
,您再次声明(并且确实传递了)。因此,只需从扩展列表中删除
RouteComponentProps


正如我们所看到的,使用包装的React组件时的类型错误可能有许多不同的原因。我希望我已经给了您一些关于如何自己追踪未来此类错误的知识。

谢谢。这就解决了这个简单情况下的问题,其中主要元素是使用
with root
的元素。但在更复杂/真实的场景中,我有多个级别的组件,我仍然会遇到这个错误。有什么想法吗?(将尝试组合一个示例)我需要查看示例。我已在此处发布了一个示例:。谢谢,非常感谢。这真是太棒了。喜欢你询问type.PR的方式,用root来修复上游的
。谢谢。这就解决了这个简单情况下的问题,其中主要元素是使用
with root
的元素。但在更复杂/真实的场景中,我有多个级别的组件,我仍然会遇到这个错误。有什么想法吗?(将尝试组合一个示例)我需要查看示例。我已在此处发布了一个示例:。谢谢,非常感谢。这真是太棒了。喜欢你询问type.PR的方式,用root修复