如何从另一个组件自定义输入组件?
对于此输入组件,我想将其作为其他组件的基本组件。
import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";
export const TextField: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
props
) => {
return (
<Input type="text" {...props} />
);
};
const Input = styled.input`
${({ theme }) => css`
width: 100%;
color: ${theme.color.text};
`}
`;
这是一种定制的样式,并添加了一些样式:
import React from "react";
import styled, { css } from "styled-components";
import { TextField } from "~/components";
export type ProductFieldProps = {
name: string;
value?: string;
};
type ProductFieldTypeProps = {
textAlign: string;
};
export const ProductField: React.FC<ProductFieldProps> = ({
name,
value,
}) => {
return (
<StyledTextField textAlign="center">
<TextField name={name} value={value} />
</StyledTextField>
);
};
const StyledTextField = styled(TextField)`
${({ theme }) => css<ProductFieldTypeProps>`
&:hover {
border-color: ${theme.color.hover};
}
`}
`;
使用新的组件productfield时,
# pages/index.tsx
import { ProductField } from "~/components";
...
return {
<>
<ProductField name="aaa" value="bbb" />
</>
}
我遇到了一个服务器错误:
错误:输入是一个void元素标签,不得有
richy
也不使用dangesslySetInnerhtml
。
如何正确设置?
For this input component, I want to make it as a basic component for other components.
import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";
export const TextField: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
props
) => {
return (
<Input type="text" {...props} />
);
};
const Input = styled.input`
${({ theme }) => css`
width: 100%;
color: ${theme.color.text};
`}
`;
This is a customized one and add some styles:
import React from "react";
import styled, { css } from "styled-components";
import { TextField } from "~/components";
export type ProductFieldProps = {
name: string;
value?: string;
};
type ProductFieldTypeProps = {
textAlign: string;
};
export const ProductField: React.FC<ProductFieldProps> = ({
name,
value,
}) => {
return (
<StyledTextField textAlign="center">
<TextField name={name} value={value} />
</StyledTextField>
);
};
const StyledTextField = styled(TextField)`
${({ theme }) => css<ProductFieldTypeProps>`
&:hover {
border-color: ${theme.color.hover};
}
`}
`;
When use the new component ProductField,
# pages/index.tsx
import { ProductField } from "~/components";
...
return {
<>
<ProductField name="aaa" value="bbb" />
</>
}
I got a server error:
Error: input is a void element tag and must neither have
children
nor usedangerouslySetInnerHTML
.
How to set correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
样式组件
为输入组件设计样式。您可以在这条线上看到您正在将样式应用于
textfield
component:而在新的
productfield
component 您导入的组件,样式版本和原始版本和原始版本和nest他们彼此之间:这本质上意味着您将两个输入字段彼此嵌套,这是不允许的,因此错误:
一种方法是简单地删除嵌套
textfield
这样的方法,然后将所有道具应用于样式的一个:另一个解决方案是直接导出并使用样式版本,因为附加包装器似乎多余。
更多信息有关如何使用样式组件的定制组件样式
You are using
styled-components
to style your Input component.You can see on this line that you are applying styles to the
TextField
component:And yet in the new
ProductField
component you import both, the styled version and the original one and nest them in each other:This essentially means you are nesting two input field into each other, which is not allowed and hence the error:
One way to approach this is to simply remove the nested
TextField
like so, and apply all the props to the styled one instead:Another solution would be to just export and use the styled version directly because the additional wrapper seems redundant.
More info on how to style custom components with styled-components