Skip to content

Commit

Permalink
Fix several breaking changes
Browse files Browse the repository at this point in the history
- didMount lifecycle was not working properly
- didUpdate lifecycle prevProps was undefined
  • Loading branch information
tommymm committed Apr 13, 2022
1 parent 09ae8ee commit 4151cad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
34 changes: 12 additions & 22 deletions src/actions/initComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'`,
);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/withInitAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default (p1, p2, p3) => {
const {
__componentInitState: { initValues },
__initComponent,
} = [prevProps];
} = prevProps;
const {
__componentInitState: { initValues: newInitValues, prepareKey },
} = this.props;
Expand Down

0 comments on commit 4151cad

Please sign in to comment.