reactjs - React Typescript-fel i TextArea: Typen 'string' kan inte tilldelas typen number
<textarea rows='3'
className='form-control'
placeholder='Enter About your description'/>
Den förväntade typen kommer från egenskapen 'rows' som deklareras här på typen 'DetailedHTMLProps, HTMLTextAreaElement>'
Svar #1
Prova detta:
<textarea rows={3}
className='form-control'
placeholder='Enter About your description'/>
Svar #2
Attributet rows kräver ett nummer, inte en sträng. Du måste också uppdatera värdet på textarea på detta sätt:
<textarea
name="description"
value={formValues.description}
rows={3}
className='form-control'
placeholder='Enter About your description'
onChange={evt=>handleInputChange(evt)}/>
Funktionen handleInputChange nedan:
// 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 })
}
Kolla in React-formulär för mer information
