From 52276ae09b61d79d141908241e23f6198032de1a Mon Sep 17 00:00:00 2001 From: Tommy Cheung Date: Wed, 13 Apr 2022 11:53:35 +0200 Subject: [PATCH] Fix breaking changes regarding the server side rendering --- package.json | 2 +- src/actions/initComponent.js | 18 +++++++++++++++--- src/withInitAction.js | 6 +++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4cf6795..7615f70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mediamonks/react-redux-component-init", - "version": "2.0.5", + "version": "2.0.6", "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 effd4cc..f3b12c9 100644 --- a/src/actions/initComponent.js +++ b/src/actions/initComponent.js @@ -2,7 +2,6 @@ import propNameValuesToObject from '../utils/propNameValuesToObject'; import { MODE_INIT_SELF, MODE_PREPARE } from '../initMode'; import PrepareValidationError from '../PrepareValidationError'; import { INIT_SELF_NEVER } from '../initSelfMode'; - import { INIT_COMPONENT } from './actionTypes'; /** @@ -25,7 +24,7 @@ import { INIT_COMPONENT } from './actionTypes'; * is generated by the `getPrepareKey()` util. * @param {object} [options] Additional options * @param {string} options.caller The function from which initialization is triggered. Valid options - * are 'prepareComponent', 'didMount', 'didUpdate' + * are 'prepareComponent', 'didMount', 'willMount', 'willReceiveProps' * @returns {function} A thunk function that should be passed directly to the Redux `dispatch` * function. */ @@ -61,7 +60,7 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch shouldCallInitAction = !!initAction && !isPrepared; shouldCallInitActionClient = false; break; - case 'didMount': + case 'constructor': errorNotPrepared = !!initAction && mode === MODE_PREPARE && !allowLazy; shouldCallInitAction = !!initAction && @@ -71,6 +70,19 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch (mode === MODE_PREPARE && !isPrepared && allowLazy)); shouldCallInitActionClient = false; break; + case 'didMount': + errorNotPrepared = false; + shouldCallInitAction = + !!initAction && + // mounted on the client (after first render), lazy 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; + break; case 'didUpdate': // reinitialize is checked in withInitAction errorNotPrepared = false; diff --git a/src/withInitAction.js b/src/withInitAction.js index 446a7c6..a965024 100644 --- a/src/withInitAction.js +++ b/src/withInitAction.js @@ -152,12 +152,16 @@ export default (p1, p2, p3) => { const { __modeInitSelf, - __componentInitState: { isPrepared }, + __componentInitState: { isPrepared, initValues, prepareKey }, } = props; this.state = { initializedOnce: !__modeInitSelf && isPrepared && !initActionClient, }; + + this.props + .__initComponent(initValues, prepareKey, { caller: 'constructor' }) + .catch(this.handleInitError); } componentDidMount() {