reactjs - 材质 UI 样式化组件如何应用样式
问题描述
Styled with styled = components using MaterialUI
在 LoginTextField 中出现以下错误。
我不知道如何解决它。
errormessage
[ts] 类型'{宽度:数字;}' 缺少来自类型 'Pick & Partial<...>、“label” | 的以下属性 ... 283 更多... | "width">': style, ref, className, onFocus 等 7 个。[2740]
环境
Typescript ,
React.js(Hooks),
Material UI,
styled-component,
// styleButton.tsx
import { Button,TextField } from "@material-ui/core";
import styled from "styled-components";
export const LoginTextField = styled(TextField)<{ width: number }>`
width: ${(props) => props.width};
`
// index.tsx
import React, { FC, useState } from "react";
import styled from "styled-components";
import { LoginTextField } from "../components/styleButton"
export const Login: FC = () => {
return (
<LoginTextField width={600}></ LoginTextField>
)
解决方案
试试这个,而不是 styleButton.tsx
import { Button, TextField, TextFieldProps } from "@material-ui/core";
import styled from "styled-components";
//type aliases
type StyledTextFieldProps = TextFieldProps && { width: number };
export const LoginTextField = styled(TextField)`
width: ${(props: StyledTextFieldProps) => props.width}
` as React.ComponentType<StyledTextFieldProps>;
推荐阅读
- c - 如何使用引用多个表的 WHERE 子句删除 sqlite 中的行?
- php - 当我的 WordPress 插件加载时,如何加载非主 PHP 文件?
- c# - 如何在 SQL Server 2012 存储过程中声明表变量?
- android - 如何将 Keras LSTM 模型用于 android?
- python-3.x - 如何自定义 ItemExporter 以不覆盖 Scrapy 上的输出文件
- reactjs - 在函数助手中调用 redux 操作
- javascript - 我的列表的下一个和上一个按钮不起作用
- amazon-web-services - AWS Cognito 和自定义角色
- c++ - 如何在 c++ builder 中动态声明 c# dll 函数
- node.js - Sequelize 和在关联中指定外键的问题