【问题标题】:React Hook useEffect has a missing dependency:propsReact Hook useEffect 缺少依赖项:props
【发布时间】:2020-11-09 08:29:47
【问题描述】:

我目前正在构建松弛克隆。我想在组件安装时检查用户是否已经登录,首先,如果他将其主页推送到登录页面。我使用 react hooks 来检查而不是 componentDidMount,还使用 ​​redux 来存储使用 mapDispatchToProps 的用户的登录状态。

我收到警告

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();

【问题讨论】:

  • 这可能对你有帮助link

标签: reactjs react-redux react-hooks


【解决方案1】:

您需要将props 作为依赖项传递。如果您有很多 props,这可能会导致性能影响。最好是 destructure 你的 props 并传入依赖数组。

你的情况是这样的:

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])

这就是警告想要表达的意思

【讨论】:

  • 非常感谢它工作正常。但是当添加道具作为依赖数组时。它进入无限循环
  • 您不必传递props,您只需传递useEffect 中所需的那些依赖项。再次查看代码。我是如何添加的
  • @suresh user 是作为来自 redux 的 prop 传递的,所以你肯定希望它在你的依赖数组中。实际上它是 anything 钩子可能会更新您不想要的依赖数组。解构并尽可能具体。
  • 谢谢,现在我清楚这个useeffect()的东西了
【解决方案2】:

这是 eslint-plugin-react-hooks 警告。 它告诉你钩子依赖于 useEffect 中的函数调用 您可以使用代码顶部的此注释 /* eslint-disable react-hooks/exhaustive-deps */ 告诉 ESLint 忽略对您的钩子的检查。

【讨论】:

  • 建议某人盲目地忽略警告可能不是一件好事。如果没有任何其他背景或经验,您会忽略汽车中的低燃油警告吗?
  • 比较不正确忽略本例有副作用
  • 添加 eslint 禁用应该是最后的手段或仅使用 - 如果您真的确信您“知道自己在做什么”,它应该总是附有关于为什么他们禁用该行的规则的评论。更天真的开发人员可能会这样做,没有意识到如果他们稍后更新依赖项可能会改变的效果,但是由于禁用了规则,linter 不会抱怨,他们的代码将无法工作,他们不会知道为什么(除非他们已经明智了)。
猜你喜欢
  • 2020-12-29
  • 2020-11-22
  • 2021-02-23
  • 2019-10-24
  • 2020-10-26
  • 2021-12-23
  • 2020-03-07
  • 2020-02-25
  • 2020-06-11
相关资源
最近更新 更多