[英]React Hook useEffect has a missing dependency:props
I am currently building slack clone.我目前正在构建松弛克隆。 I want to check whether the user is already logged in whenever the component mounts, first, if he is then push it home page else to the login page.
我想在组件安装时检查用户是否已经登录,首先,如果他是然后将它的主页推送到登录页面。 I use react hooks to check instead of
componentDidMount
and also redux to store the login state of the user using mapDispatchToProps
.我使用 react 钩子来检查而不是
componentDidMount
并且还使用 redux 来使用mapDispatchToProps
存储用户的登录状态。
I am getting the warning我收到警告
import React,{useEffect} from 'react'; import ReactDOM from 'react-dom'; import App from './components/App'; import Login from './components/Auth/login' import Register from './components/Auth/register' import reportWebVitals from './reportWebVitals'; import {BrowserRouter as Router,Switch,Route,useHistory,withRouter} from 'react-router-dom' import 'semantic-ui-css/semantic.min.css' import firebase from './firebase' import { createStore } from 'redux' import { Provider,connect} from 'react-redux' import { composeWithDevTools } from 'redux-devtools-extension' import rootReducer from './reducers/index'; import * as actions from './actions/index'; import Spinner from './spinner' const store=createStore(rootReducer,composeWithDevTools()) const Root=(props)=>{ const history=useHistory() useEffect(() => { console.log("1") firebase.auth().onAuthStateChanged(user=>{ if(user){ props.setUser(user); history.push('/home') } else{ history.push('/login') props.clearUser(); } }) },[history.location.hash,history]) return props.isLoading ? <Spinner/>:( <Switch> <Route exact path="/home" component={App} /> <Route path="/login" component={Login} /> <Route path="/register" component={Register} /> </Switch> ) } const mapStateToProps=state=>{ return { isLoading:state.user.isLoading, user:state.user.currentuser }} const mapDispatchToProps=dispatch=>{ return{ setUser:(user)=>dispatch(actions.SETuser(user)), clearUser:()=>dispatch(actions.CLEARuser()) } } const RootwithAuth=withRouter(connect(mapStateToProps,mapDispatchToProps)(Root)) ReactDOM.render( <Provider store={store}> <Router> <RootwithAuth/> </Router> </Provider>, document.getElementById('root') ); reportWebVitals();
You need to pass props
as dependency.您需要将
props
作为依赖项传递。 Which could lead to performance effect if you have lot of props
.如果你有很多
props
这可能会导致性能影响。 Better would be destructure
your props and pass in dependency array.更好的是
destructure
你的道具并传入依赖数组。
In your case it would be like this:在你的情况下,它会是这样的:
const {setUser, clearUser} = props;
useEffect(() => {
console.log("1")
firebase.auth().onAuthStateChanged(user=>{
if(user){
setUser(user);
history.push('/home')
}
else{
history.push('/login')
clearUser();
}
})
},[history.location.hash,setUser,clearUser])
This is what warning trying to say这就是警告试图说的
It's eslint-plugin-react-hooks warning.这是 eslint-plugin-react-hooks 警告。 It's telling you that hook depends on function call in useEffect you can use this comment top of code
/* eslint-disable react-hooks/exhaustive-deps */
to tell ESLint to ignore the checking for your hook.它告诉您钩子取决于 useEffect 中的函数调用,您可以使用代码顶部的注释
/* eslint-disable react-hooks/exhaustive-deps */
告诉 ESLint 忽略对钩子的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.