useEffect挂钩无限运行

时间:2020-07-10 10:18:15

标签: reactjs use-effect

我正在尝试使用react和redux创建note应用程序。 这是我的代码 mainComponenet.js // displayNotes用于从服务器获取注释。

const mapStateToProps = (state) => {
    return {
        user: state.user,
        notes: state.notes
    }
}
const mapDispacherToProps = (disptach) => ({
    displayNotes: () => disptach(tempDisplayNotes()),
})

const Main = (props) => {
  
   
    const HomeComponenet = ({ match }) => {
        return (
            <TodoPage user={props.user.user}
             notes = {props.notes.notes}
              logout = {props.logOut} 
              deleteNotes = {props.deleteNotes}
              displayNotes = {props.displayNotes}/>
        )
    }
  
    return (
        <Switch>
            <Route path="/logIn" component={login}></Route>
            <Route path="/signUp" component={signup}></Route>
            <Route exact path="/user/home" component={HomeComponenet}></Route>
            <Route path="/user/profile" component={UserProfileComponent} />
            <Route path = "/user/home/:id/edit" component = {EditNotesFun}/>
            <Route path = "/user/home/addNote" component = {AddNoteFun}/>
            <Redirect to="/home"></Redirect>
        </Switch>
    )
}

export default withRouter(connect(mapStateToProps, mapDispacherToProps)(Main));

这是todoPage.js,其中useEffect()无限运行。

let TodoPage = (props) => {
        const history = useHistory();
        useEffect(()=>{
            props.displayNotes()
        } , [])
      
        const { classes } = props;
        return (
            <div>
               </div>       
        )
    }

    export default withStyles(Style)(TodoPage);

0 个答案:

没有答案