简体   繁体   English

React Hook useEffect 缺少依赖项:props

[英]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.

相关问题 React Hook useEffect 缺少依赖项:&#39;props&#39; - React Hook useEffect has a missing dependency: 'props' React Hook useEffect 缺少依赖项 Props - React Hook useEffect has a missing dependency Props UseEffect - React Hook useEffect 缺少依赖项: - UseEffect - React Hook useEffect has a missing dependency: React Hook useEffect 缺少对 useEffect 的依赖 - React Hook useEffect has a missing dependency with useEffect useEffect 只有一个依赖项/React Hook useEffect 缺少一个依赖项:&#39;props&#39; - useEffect with only one dependency / React Hook useEffect has a missing dependency: 'props' 使用 React Hook 传递道具时,React Hook useEffect 缺少依赖项 - React Hook useEffect has a missing dependency when passing props using React Hook React Hook useEffect 缺少依赖项:&#39;props&#39; 由于 useEffect 中有一行 - React Hook useEffect has a missing dependency: 'props' due to one line in useEffect 反应 | React Hook useEffect 缺少依赖项 - React | React Hook useEffect has a missing dependency React Hook useEffect 缺少依赖项:'props'。 包括它或删除依赖项数组。 useEffect 中的道具没有数据 - React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. props in useEffect have no data React Hook useEffect 缺少依赖项:&#39;props.myObj&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'props.myObj'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM