From 572ab142953de444d6cf68d215ffd75f254e619b Mon Sep 17 00:00:00 2001 From: Tommy Cheung Date: Tue, 12 Apr 2022 17:13:08 +0200 Subject: [PATCH] Update lifecycle --- package.json | 2 +- src/actions/initComponent.js | 2 +- src/withInitAction.js | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 8fb8956..b206130 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mediamonks/react-redux-component-init", - "version": "2.0.2", + "version": "2.0.0", "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 7bfb26c..2bc6f03 100644 --- a/src/actions/initComponent.js +++ b/src/actions/initComponent.js @@ -74,7 +74,7 @@ export default (Component, initValues, prepareKey, { caller } = {}) => (dispatch // mounted on the client (after first render) initSelf !== INIT_SELF_NEVER; break; - case 'getDerivedStateFromProps': + case 'didUpdate': errorNotPrepared = false; shouldCallInitAction = !!initAction; shouldCallInitActionClient = !!initActionClient; diff --git a/src/withInitAction.js b/src/withInitAction.js index e18aabb..0b4bee5 100644 --- a/src/withInitAction.js +++ b/src/withInitAction.js @@ -173,23 +173,27 @@ export default (p1, p2, p3) => { __componentInitState: { selfInitState }, } = newProps; if (selfInitState) { - this.setState(() => ({ + return { initializedOnce: true, - })); + }; } + return null; + } + + componentDidUpdate(prevProps) { if (initProps.length && reinitialize) { const { __componentInitState: { initValues }, __initComponent, - } = this.props; + } = [prevProps]; const { __componentInitState: { initValues: newInitValues, prepareKey }, - } = newProps; + } = this.props; if (initValues !== newInitValues) { __initComponent(newInitValues, prepareKey, { - caller: 'getDerivedStateFromProps', + caller: 'didUpdate', }).catch(this.handleInitError); } }