简体   繁体   English

尝试在React / TypeScript应用中使用usetState时应用中断

[英]App breaking when trying to use usetState in React/TypeScript app

Expected 预期

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功能。

Results 结果

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.

相关问题 尝试在 typescript 中使用 React useref 时出错 - Error when trying to use React useref in typescript 尝试将打字稿库用于 javascript express 应用程序 - trying to use typescript library into javascript express app 尝试创建React应用时出现纱线错误 - Yarn errors when trying to create a React app 为什么 typescript 在尝试使用 ref 反应时抱怨? - why is typescript complaining when trying to use ref in react? 在TypeScript React Web应用程序内使用JavaScript库 - Use javascript library inside of TypeScript React web app 在带有打字稿的 create-react-app 中使用 mozilla pdf.js - Use mozilla pdf.js in create-react-app with typescript 你如何配置一个反应应用程序来使用 typescript? - How do you configure a react app to use typescript? 尝试将 jest 与 create-react-app 和 typescript 一起使用。 获取错误:开玩笑:无法解析 TypeScript 配置文件...找不到模块“ts-node” - Trying to use jest with create-react-app and typescript. Get error: Jest: Failed to parse the TypeScript config file... Cannot find module 'ts-node' 无法使用打字稿创建反应应用程序 - unable to create react app with typescript 试图请求用户在 React 应用程序中使用相机和麦克风的权限 - Trying to ask for users permission to use camera and microphone in a React app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM