Jen*_*fer 2 javascript typescript ecmascript-6 reactjs react-hooks
What is the problem with below code? I got warning by typescript when use useState
import * as React, { useState } from 'react'
const useForm = (callback: any | undefined) => {
const [inputs, setInputs] = useState({}) //error: Cannot find name 'useState'.ts(2304)
const handleInputChange = event => {
event.persist()
setInputs(inputs => ({
...inputs,
[event.target.name]: event.target.value,
}))
}
return {
handleInputChange,
inputs,
}
}
export default useForm
Run Code Online (Sandbox Code Playgroud)
您的导入语句无效。
您可以React像这样导入所有内容:
import * as React from 'react';
Run Code Online (Sandbox Code Playgroud)
以及useState来自React对象的访问:
const [inputs, setInputs] = React.useState({})
Run Code Online (Sandbox Code Playgroud)
或导入React为默认值,仅部分导入其他名称:
import React, { useState } from 'react'
Run Code Online (Sandbox Code Playgroud)
和useState以前一样的使用:
const [inputs, setInputs] = useState({});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4124 次 |
| 最近记录: |