如何使用React添加样式?
在自定义的输入组件中,
import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";
type InputTypeProps = {
disabled: boolean;
};
export const MyInput: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
disabled,
props
) => {
return <Input type="text" disabled={disabled} {...props} />;
};
const Input = styled.input`
${() => css<InputTypeProps>`
background-color: ${({ disabled }) => (disabled == false ? "white" : "gray")};
`}
`;
当使用此组件时,
import { MyInput } from "~/components";
const Home: NextPage = () => {
const disabled = false;
return (
<MyInput name="input1" disabled={disabled} />
);
};
当我将禁用
设置为true或false时,它会显示灰色。似乎这种方式不起作用。是否可以处理此HTML属性以以反应方式改变?
In a customized input component
import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";
type InputTypeProps = {
disabled: boolean;
};
export const MyInput: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
disabled,
props
) => {
return <Input type="text" disabled={disabled} {...props} />;
};
const Input = styled.input`
${() => css<InputTypeProps>`
background-color: ${({ disabled }) => (disabled == false ? "white" : "gray")};
`}
`;
When use this component as
import { MyInput } from "~/components";
const Home: NextPage = () => {
const disabled = false;
return (
<MyInput name="input1" disabled={disabled} />
);
};
It alwasy show gray color when I set disabled
to true or false. It seems this way doesn't work. Is it possible to handle this html property to change in React way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改此。
Change to this.