繁体   English   中英

TextArea 中的 React Typescript 错误:类型“字符串”不可分配给类型编号

[英]React Typescript error in TextArea : Type 'string' is not assignable to type number

<textarea rows='3' 
  className='form-control' 
  placeholder='Enter About your description'/>

预期的类型来自属性 'rows',它在类型 'DetailedHTMLProps, HTMLTextAreaElement>' 上声明

截屏

尝试这个:

<textarea rows={3} 
  className='form-control' 
  placeholder='Enter About your description'/>

row属性需要一个数字,而不是一个字符串。 您还需要像这样更新 textarea 的值:

<textarea 
  name="description"
  value={formValues.description}
  rows={3} 
  className='form-control' 
  placeholder='Enter About your description'
  onChange={evt=>handleInputChange(evt)}/>

下面的 handleInputChange 函数:

// You can define an interface to type the formValues object like this
interface formValuesInterface {
    description: string
}

// Keep the description value in the state like this
const [formValues, setFormValues] = React.useState<formValuesInterface>({description: ''})

const handleInputChange = (evt: React.ChangeEvent<HTMLInputElement>)=> {
    const { name, type } = evt.target
    const value = target.type === 'checkbox' ? target.checked : target.value;
    setFormValues({ ...formValues, [name]: value })
}

查看React 表单以获取更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM