【发布时间】:2023-02-04 23:38:18
【问题描述】:
我有一个子输入组件和我设置值的父组件。
父组件
const \[dummyText, setDummyText\] = useState('')
return (
<Input
{...props}
id="first-name"
label="First name"
type="text"
placeholder="Please enter your first name"
value={dummyText}
onChange={(value) => setDummyText(value.target.value)}
/>
)
子输入组件
import React, { InputHTMLAttributes, forwardRef } from 'react'
export type Props = {} & InputHTMLAttributes<HTMLInputElement>
const FormInput: React.FC<Props> = forwardRef<HTMLInputElement, Partial<Props>>(
({ ...props }, ref) => <input ref={ref} {...props} />
)
FormInput.displayName = 'FormInput'
export default FormInput
我想在子组件中添加按钮,点击我需要清除该值。
问题是我想在子组件中有明确的功能,我不想为父组件中的每个 <Input 编写额外的代码,无论我在子组件中做什么,我都无法覆盖 props.value。
有没有人有解决这个问题的经验?
【问题讨论】:
-
你能分享你的
Input组件吗? -
@RubenSmn 我更新了我的代码。发件人