如何在打字稿中将输入属性传递给组件内部的组件
我正在学习 React 和 TypeScript,并且正在使用样式组件构建表单。
我想将 onChange、类型和值等属性传递给我的 <Ìnput/>
组件,但我不知道如何执行此操作。
我有一个
页面组件,如果我使用我的 <Ìnput />
组件,如下所示
import { Container } from './styles';
import Header from '../../components/Header';
import Input from '../../components/Input';
const FormData: React.FC = () => {
return (
<Container>
<Header>Your Data</Header>
<fieldset>
<legend>Personal Info</legend>
<Input label='Nome' placeholder='Duck ' onChange={ () => {} } />
{...others inputs here}
</fieldset>
<fieldset>
<legend>Profissional Info</legend>
<Input label='curriculum' placeholder='123.412.123-44' type={textarea}/>
</fieldset>
</Container>
);
};
export default FormData;
: 组件看起来像这样:
import { Label, InputField } from './styles';
interface InputProps {
label: string;
placeholder?: string;
}
function Input({ label, placeholder }: InputProps) {
return (
<Label>
{label}
<InputField placeholder={placeholder} />
</Label>
);
}
export default Input;
我尝试 ref 和forwardRef,然后将其传递给我的样式组件,但它不起作用。
在我的
样式组件中,我想传递在 上使用的通用
上收到的 onChange、value 和 type 属性
在我的
样式组件中,我想做这样的事情:
import styled from 'styled-components';
const { type } = this.props // Can i do that?
export const InputField = styled.type`
width: 100%;
margin-left: 0.3rem;
`;
I'm learning react and typescript and i'm building a form using styled components.
I want to pass props like onChange, type and value to my <Ìnput />
component but i dont know how to do this.
I have a <FormData />
page component were i use my <Ìnput />
component like this:
import { Container } from './styles';
import Header from '../../components/Header';
import Input from '../../components/Input';
const FormData: React.FC = () => {
return (
<Container>
<Header>Your Data</Header>
<fieldset>
<legend>Personal Info</legend>
<Input label='Nome' placeholder='Duck ' onChange={ () => {} } />
{...others inputs here}
</fieldset>
<fieldset>
<legend>Profissional Info</legend>
<Input label='curriculum' placeholder='123.412.123-44' type={textarea}/>
</fieldset>
</Container>
);
};
export default FormData;
and my <Ìnput />
component looks like this:
import { Label, InputField } from './styles';
interface InputProps {
label: string;
placeholder?: string;
}
function Input({ label, placeholder }: InputProps) {
return (
<Label>
{label}
<InputField placeholder={placeholder} />
</Label>
);
}
export default Input;
I tried to ref and forwardRef and then pass it on props to my styled component but it didnt work.
In my <InputField />
styled component i would like to pass onChange, value and type props received on my generic <Input />
used on <FormData />
And inside my <InputField />
styled component i would like to do something like this:
import styled from 'styled-components';
const { type } = this.props // Can i do that?
export const InputField = styled.type`
width: 100%;
margin-left: 0.3rem;
`;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InputField
样式组件使用input
标记:input
markup for theInputField
styled component :