[英]App breaking when trying to use usetState in React/TypeScript app
Being able to use useState
to set an email and password var inside of a functional React component. 能够使用
useState
在功能性React组件内设置电子邮件和密码var。
Then using onChange on the inputs to change those variables, then sent the results of those variables up to the parent class via the signIn
function. 然后使用对输入的onChange改变这些变量,然后经由发送的那些变量的结果到父类
signIn
功能。
There is a typescript error highlighted over the onChange
, and the app breaks. 在
onChange
突出显示了一个打字稿错误,并且应用程序中断了。
Code 码
import React, {useState} from 'react'
import {
AuthArea,
AuthInputs,
CreateNewAccount,
FunctionButton
} from '../../styles'
interface IProps {
signIn(email: string, password: string): void
}
export default ({ signIn }: IProps) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<AuthArea>
<h1>Sign In</h1>
<AuthInputs>
<input
type="text"
placeholder="Email"
value={email}
onChange={setEmail}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={setPassword}
/>
<FunctionButton onClick={() => signIn(email, password)}>
Sign In
</FunctionButton>
<a href="#">Forgot Password?</a>
</AuthInputs>
<CreateNewAccount>
Create New Account
</CreateNewAccount>
</AuthArea>
)
}
The error 错误
(JSX attribute) React.InputHTMLAttributes.onChange?: ((event: React.ChangeEvent) => void) |
(JSX属性)React.InputHTMLAttributes.onChange ?:((事件:React.ChangeEvent)=>无效)| undefined Type 'Dispatch>' is not assignable to type '(event: ChangeEvent) => void'.
未定义类型'调度>不是分配给输入“(事件:的ChangeEvent)=>无效”。 Types of parameters 'value' and 'event' are incompatible.
参数“值”和“事件”的类型不兼容。 Type 'ChangeEvent' is not assignable to type 'SetStateAction'.
类型'ChangeEvent'不能分配给类型'SetStateAction'。 Type 'ChangeEvent' is not assignable to type '(prevState: string) => string'.
类型'ChangeEvent'不能分配给类型'(prevState:string)=> string'。 Type 'ChangeEvent' provides no match for the signature '(prevState: string): string'.ts(2322) index.d.ts(1976, 9): The expected type comes from property 'onChange' which is declared here on type 'DetailedHTMLProps, HTMLInputElement>'
类型'ChangeEvent'不提供签名'(prevState:string):string'的匹配项。ts(2322)index.d.ts(1976,9):预期的类型来自属性'onChange',在此声明该类型'DetailedHTMLProps,HTMLInputElement>'
This is how you setState in onChange: 这是在onChange中设置setState的方式:
<input
type="text"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
Another version with extracted out function and using currying method for re-usability is as below: 另一个具有提取功能并使用currying方法可重用的版本如下:
export default ({ signIn }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleChange = setterFunc => e => {
const value = e.target.value;
setterFunc(value);
}
return (
<>
<input
type="text"
placeholder="Email"
value={email}
onChange={handleChange(setEmail)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={handleChange(setPassword)}
/>
</>
)
}
Here's live version: https://stackblitz.com/edit/react-rf4bhw 这是实时版本: https : //stackblitz.com/edit/react-rf4bhw
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.