void)' .ts(2339) 上不存在"-6ren"> void)' .ts(2339) 上不存在"-我正在尝试使用自定义钩子(Hook)管理表单,所以我有这个代码 FormHook.tsx: import { useState } from 'react'; interface ICampos {-6ren">
gpt4 book ai didi

reactjs - React Custom Hook with Typescript Type error "Property ' x' 在类型 'interface | (({ target }: any) => void)' .ts(2339) 上不存在"

转载 作者:行者123 更新时间:2023-12-04 11:30:41 27 4
gpt4 key购买 nike

我正在尝试使用自定义钩子(Hook)管理表单,所以我有这个代码
FormHook.tsx:

import { useState } from 'react';

interface ICampos {
name: string;
email: string;
password: string;
}


const useForm = (initialState: ICampos) => {

const [values, setValues] = useState(initialState);


const handleInputChange = ({ target }: any) => {
setValues({
...values,
[target.name]: target.value
})
};

return [values, handleInputChange];
}

export default useForm
FormWithCustomHook.tsx:
import React from 'react'
import './effects.css'
import useForm from '../../hooks/useForm';

interface ICampos {
name: string;
email: string;
password: string;
}

const FormWithCustomHook = () => {

const [formValues, handleInputChange] = useForm({
name: '',
email: '',
password: ''
});

const { name, email, password } = formValues;



return (
<>
<h1> FormWithCustomHook </h1>
<hr />

<div className="form-group">
<input
type="text"
name="name"
className="form-control"
placeholder="Tu nombre"
autoComplete="off"
value={name}
onChange={handleInputChange} />
</div>

<div className="form-group">

<input
type="email"
name="email"
className="form-control"
placeholder="email@ejemplo.com"
autoComplete="off"
value={email}
onChange={handleInputChange} />
</div>

<div className="form-group">

<input
type="password"
name="password"
className="form-control"
placeholder="*****"
autoComplete="off"
value={password}
onChange={handleInputChange} />
</div>

</>
)
}

export default FormWithCustomHook;
我在 FormWithCustomHook.tsx 上遇到了这个错误:
const { name, email, password } = formValues;
它仅在电子邮件和密码上标记错误:
'ICampos | 类型不存在'属性'电子邮件' (({ target }: any) => void)'.ts(2339)'
在我的 onChange 输入中说:
输入'ICampos | (({ target }: any) => void)' 不可分配给类型 '((event: ChangeEvent) => void) |不明确的'。
类型 'ICampos' 不可分配给类型 '(event: ChangeEvent) => void'。
类型“ICampos”不匹配签名“(事件:ChangeEvent):void”.ts(2322)
index.d.ts(2092, 9): 预期的类型来自属性 'onChange',它在类型 'DetailedHTMLProps '' 上声明
我试图在 customhook.tsx 上添加类型,但我真的不明白这个错误

最佳答案

钩子(Hook)不知道数组的顺序。所以它可能是 ICampos | HandlerHandler | ICampos .您在这里有两个选择:
您可以在钩子(Hook)上键入返回数组:

const useForm = (
initialState: ICampos
): [ICampos, ({ target }: any) => void] => {
const [values, setValues] = useState<ICampos>(initialState);

const handleInputChange = ({ target }: any) => {
setValues({
...values,
[target.name]: target.value
});
};

return [values, handleInputChange];
};
或者你可以返回一个对象而不是一个数组。我更喜欢这个,因为我不喜欢输入数组。
import { useState } from "react";

interface ICampos {
name: string;
email: string;
password: string;
}

const useForm = (initialState: ICampos) => {
const [values, setValues] = useState<ICampos>(initialState);

const handleInputChange = ({ target }: any) => {
setValues({
...values,
[target.name]: target.value
});
};

return { values, handleInputChange };
};

export default useForm;
import React from "react";
import "./effects.css";
import useForm from "./useForm";

interface ICampos {
name: string;
email: string;
password: string;
}

const FormWithCustomHook = () => {
const { values, handleInputChange } = useForm({
name: "",
email: "",
password: ""
});

const { name, email, password } = values;

return (
<>
<h1> FormWithCustomHook </h1>
<hr />

<div className="form-group">
<input
type="text"
name="name"
className="form-control"
placeholder="Tu nombre"
autoComplete="off"
value={name}
onChange={handleInputChange}
/>
</div>

<div className="form-group">
<input
type="email"
name="email"
className="form-control"
placeholder="email@ejemplo.com"
autoComplete="off"
value={email}
onChange={handleInputChange}
/>
</div>

<div className="form-group">
<input
type="password"
name="password"
className="form-control"
placeholder="*****"
autoComplete="off"
value={password}
onChange={handleInputChange}
/>
</div>
</>
);
};

export default FormWithCustomHook;

关于reactjs - React Custom Hook with Typescript Type error "Property ' x' 在类型 'interface | (({ target }: any) => void)' .ts(2339) 上不存在",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62618333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com