[英]TypeScript: 'handleFirstTab' was used before it was defined @typescript-eslint/no-use-before-define
I have the following code in a React Component and TypeScript gives the following error:我在 React 组件中有以下代码,TypeScript 给出以下错误:
'handleFirstTab' was used before it was defined @typescript-eslint/no-use-before-define
If I split both functions into separate files and import them in each other then the error goes away.如果我将两个函数拆分为单独的文件并将它们相互导入,那么错误就会消失。 Is there a way via which I can have both functions in the same file without disabling
@typescript-eslint/no-use-before-define
and the error will go away.有没有一种方法可以让我在同一个文件中同时拥有这两个函数而不禁用
@typescript-eslint/no-use-before-define
并且错误将 go 消失。 Thanks.谢谢。
const handleMouseDownOnce = (): void => {
document.body.classList.remove('user-is-tabbing')
window.removeEventListener('mousedown', handleMouseDownOnce)
window.addEventListener('keydown', handleFirstTab)
}
const handleFirstTab = (e: KeyboardEvent): void => {
if (e.code === 'Tab') {
document.body.classList.add('user-is-tabbing')
window.removeEventListener('keydown', handleFirstTab)
window.addEventListener('mousedown', handleMouseDownOnce)
}
}
when you define a variable (or function) using const
or let
it won't be Hoist
as function declaration using function
keyword or using var
当您使用
const
定义变量(或函数) function
let
Hoist
使用var
if you want to have both of them in the same file you need to change your code like following:如果您想将它们都放在同一个文件中,则需要更改代码,如下所示:
function handleMouseDownOnce(): void {
document.body.classList.remove('user-is-tabbing')
window.removeEventListener('mousedown', handleMouseDownOnce)
window.addEventListener('keydown', handleFirstTab)
}
function handleFirstTab(e: KeyboardEvent): void{
if (e.code === 'Tab') {
document.body.classList.add('user-is-tabbing')
window.removeEventListener('keydown', handleFirstTab)
window.addEventListener('mousedown', handleMouseDownOnce)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.