From 4151cad24bbe73213555865ac077901ba5c10e78 Mon Sep 17 00:00:00 2001 From: Tommy Cheung Date: Wed, 13 Apr 2022 10:35:01 +0200 Subject: [PATCH] Fix several breaking changes - didMount lifecycle was not working properly - didUpdate lifecycle prevProps was undefined --- package.json | 2 +- src/actions/initComponent.js | 34 ++++++++++++---------------------- src/withInitAction.js | 2 +- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 52751c3..4cf6795 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mediamonks/react-redux-component-init", - "version": "2.0.4", + "version": "2.0.5", "description": "A library to manage async component initialization in isomorphic React/Redux applications with server-side rendering", "main": "./lib/index.js", "scripts": { diff --git a/src/actions/initComponent.js b/src/actions/initComponent.js index 2bc6f03..effd4cc 100644 --- a/src/actions/initComponent.js +++ b/src/actions/initComponent.js @@ -62,26 +62,24 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch shouldCallInitActionClient = false; break; case 'didMount': - errorNotPrepared = false; + errorNotPrepared = !!initAction && mode === MODE_PREPARE && !allowLazy; shouldCallInitAction = !!initAction && - // mounted on the client (after first render), lazy allowed - ((mode === MODE_INIT_SELF && initSelf !== INIT_SELF_NEVER && allowLazy) || + // mounted on the client (after first render), lazy not allowed + ((mode === MODE_INIT_SELF && initSelf !== INIT_SELF_NEVER && !allowLazy) || // first render, not already prepared, lazy allowed (mode === MODE_PREPARE && !isPrepared && allowLazy)); - shouldCallInitActionClient = - !!initActionClient && - // mounted on the client (after first render) - initSelf !== INIT_SELF_NEVER; + shouldCallInitActionClient = false; break; case 'didUpdate': + // reinitialize is checked in withInitAction errorNotPrepared = false; shouldCallInitAction = !!initAction; shouldCallInitActionClient = !!initActionClient; break; default: throw new Error( - `Unexpected value '${caller}' for caller. Expected one of: 'prepareComponent', 'didMount', 'didUpdate'`, + `Unexpected value '${caller}' for caller. Expected one of: 'prepareComponent', 'didMount', 'willMount', 'willReceiveProps'`, ); } @@ -155,12 +153,10 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch : Promise.resolve(); if (typeof initActionReturn.then !== 'function') { - const initActionType = initActionObjectParam ? 'initAction.prepared' : 'initAction'; - - const error = new PrepareValidationError( - `Expected ${initActionType} to return a Promise. Returned a ${typeof initActionReturn} instead. Check the initAction for "${componentId}"`, - 'init-action-must-return-promise', - { initActionType, Component: componentId, returnType: typeof initActionReturn }, + const error = new Error( + `Expected initAction${ + initActionObjectParam ? '.server' : '' + } to return a Promise. Returned an ${typeof initActionReturn} instead. Check the initAction for "${componentId}"`, ); error.isInvalidReturnError = true; throw error; @@ -174,14 +170,8 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch : Promise.resolve(); if (typeof initActionClientReturn.then !== 'function') { - const error = new PrepareValidationError( - `Expected initAction.clientOnly to return a Promise. Returned a ${typeof initActionClientReturn} instead. Check the initAction for "${componentId}"`, - 'init-action-must-return-promise', - { - initActionType: 'initAction.clientOnly', - Component: componentId, - returnType: typeof initActionClientReturn, - }, + const error = new Error( + `Expected initAction.client to return a Promise. Returned an ${typeof initActionClientReturn} instead. Check the initAction for "${componentId}"`, ); error.isInvalidReturnError = true; throw error; diff --git a/src/withInitAction.js b/src/withInitAction.js index 0b4bee5..446a7c6 100644 --- a/src/withInitAction.js +++ b/src/withInitAction.js @@ -186,7 +186,7 @@ export default (p1, p2, p3) => { const { __componentInitState: { initValues }, __initComponent, - } = [prevProps]; + } = prevProps; const { __componentInitState: { initValues: newInitValues, prepareKey }, } = this.props;