javascript - 没有未使用的变量
问题描述
在我的 useState 钩子中,我正在导入上下文;因此, setUser 将被闲置并给我和 Eslinting 警告。我不确定如何扼杀这个警告,并且已经没有办法做到这一点。如果有人在 React 中有一个建议或最佳实践来扼杀这个警告,我将不胜感激。代码如下:
import React, { useContext } from 'react'
import { Link } from 'react-router-dom'
// Material UI
import Button from '@material-ui/core/Button'
import Grid from '@material-ui/core/Grid'
import Container from '@material-ui/core/Container'
import User from './User'
// context
import { ProfileContext } from '../contexts/ProfileContext'
const Header = ({ isAuth, logout }) => {
const [user, setUser] = useContext(ProfileContext)
return (
<Container maxWidth="lg" style={{ padding: 10 }}>
<Grid container justify="space-between">
<Grid item xs={2}>
<Button color="inherit" component={Link} to="/">
Jobtracker
</Button>
</Grid>
<Grid item xs={10} container justify="flex-end">
<div>
{isAuth ? (
<>
{user && user.user.admin && (
<Button color="inherit" component={Link} to="/admin">
Admin
</Button>
)}
<Button color="inherit" component={Link} to="/profile">
Profile
</Button>
<Button color="inherit" component={Link} to="/dashboard">
Dashboard
</Button>
<Button color="inherit" onClick={logout}>
Logout
</Button>
</>
) : (
<>
<Button color="inherit" component={Link} to="/login">
Login
</Button>
<Button color="inherit" component={Link} to="/signup">
SignUp
</Button>
</>
)}
</div>
</Grid>
</Grid>
</Container>
)
}
export default Header
解决方案
当你不需要它时,你不必解构它:
const [user] = useContext(ProfileContext)
此外,如果您只需要setUser
,您可以在解构时使用comma
不带变量或常量名称的 a 跳过项目:
const [, setUser] = useContext(ProfileContext)
推荐阅读
- sql - SQL 将长整数缩短为 4 或 5 个字符的数字,带两位小数
- c++ - uint8_t 转为字符串,导致无输出 C++(初级程序员)
- android - Android,Firebase Recycler Adapter 未查询数据库
- mysql - 如果表中没有数据,如何为表中的列赋予默认值?
- objective-c - iOS 9 / 10 -> 如何检查空闲/可用空间
- deployment - Julia 语言轻量级部署
- stochastic - 随机前沿生产函数需要哪些变量来衡量公司是否正在最小化成本?
- ssas - Adventure Works:根据婚姻状况计算年销售额
- apache - 将所有 example.com/page 链接解析为 page.html 文件
- mysql - Mysql2::Error: 带有外语 msql 表的未知列