小编use*_*992的帖子

React Custom Hook with Typescript Type 错误“属性 'x' 不存在于类型 'interface | (({ target }: any) => void)'.ts(2339)”

我正在尝试使用自定义钩子管理表单,所以我有这个代码

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
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-hooks

5
推荐指数
1
解决办法
1836
查看次数

标签 统计

react-hooks ×1

reactjs ×1

typescript ×1