简体   繁体   English

使用 useffect 和 redux 时反应无限循环

[英]React infinite loop when using useffect with redux

I'm setting a radux state with an object, the get the value from the store and print it on screen我正在使用对象设置 radux 状态,从商店获取值并将其打印在屏幕上

I'm using useState and use useEffect我正在使用useState并使用useEffect

const AvatarContainer = (props) => {
const [infos, setinfos] = useState(undefined);
useEffect(() => {
    console.log('in effects')

    setinfos(() => props.userInfos)

}, [props.userInfos]);
 
props.saveUserInfos(authContext.getCachedUser())

const mapStateToProps = (state) => {
    return ({
        userInfos: state.reportingActions.userInfos,
    })
}

const mapDispatchToProps = (dispatch) => {
    return ({
        saveUserInfos: (Avatar) => dispatch(saveUserInfos(Avatar))
    })
}

reducer减速器

case 'SAVE_USER_INFOS':
   return {
       ...state,
       userInfos: Object.assign({}, action.payload.userInfos
   }

action行动

export const saveUserInfos = userInfos => {

    return ({
        type: 'SAVE_USER_INFOS',
        payload: {
            userInfos
        }
    })
}

I get this error我收到这个错误

Maximum update depth exceeded.超出最大更新深度。 This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,就会发生这种情况。 React limits the number of nested updates to prevent infinite loops. React 限制嵌套更新的数量以防止无限循环。

You need to call redux action in useEffect, like below,您需要在 useEffect 中调用 redux 操作,如下所示,

useEffect(() => {
    console.log("effect done")
    if (photo === undefined) {
        setinfos(props.userInfos)
    }
    props.saveUserInfos(authContext.getCachedUser())
}, []);

Your code goes in infinite loop because props.saveUserInfos(authContext.getCachedUser()) update your component props and when your component's props is update then it will be remount or re-render again您的代码进入无限循环,因为props.saveUserInfos(authContext.getCachedUser())更新您的组件道具,当您的组件道具更新时,它将重新安装或重新渲染

Hope this answer helps you!希望这个回答对你有帮助!

props.saveUserInfos(authContext.getCachedUser()) is executed each time the component re-renders.每次组件重新渲染时都会执行props.saveUserInfos(authContext.getCachedUser()) It triggers a redux action dispatch that manipulates redux store which in turn triggers re-render of AvatarContainer component.它触发了一个操作 redux store 的 redux action dispatch,这反过来触发了 AvatarContainer 组件的重新渲染。 Then, saveUserInfos is executed so the process repeats and effectively enters infinite loop.然后, saveUserInfos被执行,因此该过程重复并有效地进入无限循环。

To resolve your issue you need to change the logic when the actual save is done.要解决您的问题,您需要在实际保存完成后更改逻辑。 A starting point - depending on what you want to achieve - might be to move into into effect that triggers on internal state change:一个起点 - 取决于您想要实现的目标 - 可能是在内部状态更改时触发:

useEffect(() => {
    props.saveUserInfos(authContext.getCachedUser())
, [infos]);

This way, external store's state will be synchronized each time internal state changes.这样,每次内部状态更改时,外部存储的状态都会同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM