From 1aa90bef3738e1bdbc8189af91337d9d02085b46 Mon Sep 17 00:00:00 2001 From: orangemug Date: Mon, 6 Aug 2018 22:24:13 +0100 Subject: [PATCH 1/8] Removed componentWillUpdate --- src/components/fields/_ZoomProperty.jsx | 57 +++++++++++++------------ src/components/inputs/NumberInput.jsx | 6 ++- src/components/inputs/StringInput.jsx | 6 ++- src/components/layers/JSONEditor.jsx | 8 ++-- src/components/layers/LayerEditor.jsx | 10 ++--- src/components/map/OpenLayers3Map.jsx | 4 +- src/components/modals/ExportModal.jsx | 9 ++-- 7 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/components/fields/_ZoomProperty.jsx b/src/components/fields/_ZoomProperty.jsx index bca99ad..2d6baac 100644 --- a/src/components/fields/_ZoomProperty.jsx +++ b/src/components/fields/_ZoomProperty.jsx @@ -13,6 +13,30 @@ import docUid from '../../libs/document-uid' import sortNumerically from '../../libs/sort-numerically' +/** + * We cache a reference for each stop by its index. + * + * When the stops are reordered the references are also updated (see this.orderStops) this allows React to use the same key for the element and keep keyboard focus. + */ +function setStopRefs(props, state) { + // This is initialsed below only if required to improved performance. + let newRefs; + + if(props.value && props.value.stops) { + props.value.stops.forEach((val, idx) => { + if(!state.refs.hasOwnProperty(idx)) { + if(!newRefs) { + newRefs = {...state}; + } + newRefs[idx] = docUid("stop-"); + } + }) + } + + return newRefs; +} + + export default class ZoomProperty extends React.Component { static propTypes = { onChange: PropTypes.func, @@ -39,39 +63,16 @@ export default class ZoomProperty extends React.Component { componentDidMount() { this.setState({ - refs: this.setStopRefs(this.props) + refs: setStopRefs(this.props, this.state) }) } - /** - * We cache a reference for each stop by its index. - * - * When the stops are reordered the references are also updated (see this.orderStops) this allows React to use the same key for the element and keep keyboard focus. - */ - setStopRefs(props) { - // This is initialsed below only if required to improved performance. - let newRefs; - - if(props.value && props.value.stops) { - props.value.stops.forEach((val, idx) => { - if(!this.state.refs.hasOwnProperty(idx)) { - if(!newRefs) { - newRefs = {...this.state.refs}; - } - newRefs[idx] = docUid("stop-"); - } - }) - } - - return newRefs; - } - - UNSAFE_componentWillReceiveProps(nextProps) { - const newRefs = this.setStopRefs(nextProps); + static getDerivedStateFromProps(nextProps) { + const newRefs = setStopRefs(props, state); if(newRefs) { - this.setState({ + return { refs: newRefs - }) + }; } } diff --git a/src/components/inputs/NumberInput.jsx b/src/components/inputs/NumberInput.jsx index 447fab8..90cf969 100644 --- a/src/components/inputs/NumberInput.jsx +++ b/src/components/inputs/NumberInput.jsx @@ -17,8 +17,10 @@ class NumberInput extends React.Component { } } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState({ value: nextProps.value }) + static getDerivedStateFromProps(props, state) { + return { + value: props.value + }; } changeValue(newValue) { diff --git a/src/components/inputs/StringInput.jsx b/src/components/inputs/StringInput.jsx index b94a709..b5f6bac 100644 --- a/src/components/inputs/StringInput.jsx +++ b/src/components/inputs/StringInput.jsx @@ -18,8 +18,10 @@ class StringInput extends React.Component { } } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState({ value: nextProps.value || '' }) + static getDerivedStateFromProps(props, state) { + return { + value: props.value || '' + }; } render() { diff --git a/src/components/layers/JSONEditor.jsx b/src/components/layers/JSONEditor.jsx index 8cec16d..2204623 100644 --- a/src/components/layers/JSONEditor.jsx +++ b/src/components/layers/JSONEditor.jsx @@ -29,10 +29,10 @@ class JSONEditor extends React.Component { } } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState({ - code: JSON.stringify(nextProps.layer, null, 2) - }) + static getDerivedStateFromProps(props, state) { + return { + code: JSON.stringify(props.layer, null, 2) + }; } shouldComponentUpdate(nextProps, nextState) { diff --git a/src/components/layers/LayerEditor.jsx b/src/components/layers/LayerEditor.jsx index 7d7291d..6e245b3 100644 --- a/src/components/layers/LayerEditor.jsx +++ b/src/components/layers/LayerEditor.jsx @@ -79,18 +79,18 @@ export default class LayerEditor extends React.Component { this.state = { editorGroups } } - UNSAFE_componentWillReceiveProps(nextProps) { - const additionalGroups = { ...this.state.editorGroups } + static getDerivedStateFromProps(props, state) { + const additionalGroups = { ...state.editorGroups } - layout[nextProps.layer.type].groups.forEach(group => { + layout[props.layer.type].groups.forEach(group => { if(!(group.title in additionalGroups)) { additionalGroups[group.title] = true } }) - this.setState({ + return { editorGroups: additionalGroups - }) + }; } getChildContext () { diff --git a/src/components/map/OpenLayers3Map.jsx b/src/components/map/OpenLayers3Map.jsx index 113781a..a5881de 100644 --- a/src/components/map/OpenLayers3Map.jsx +++ b/src/components/map/OpenLayers3Map.jsx @@ -29,10 +29,10 @@ class OpenLayers3Map extends React.Component { const styleFunc = olms.apply(this.map, newMapStyle) } - UNSAFE_componentWillReceiveProps(nextProps) { + componentDidUpdate() { require.ensure(["ol", "ol-mapbox-style"], () => { if(!this.map) return - this.updateStyle(nextProps.mapStyle) + this.updateStyle(this.props.mapStyle) }) } diff --git a/src/components/modals/ExportModal.jsx b/src/components/modals/ExportModal.jsx index f7d997a..03c0d66 100644 --- a/src/components/modals/ExportModal.jsx +++ b/src/components/modals/ExportModal.jsx @@ -31,11 +31,10 @@ class Gist extends React.Component { } } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState({ - ...this.state, - preview: !!(nextProps.mapStyle.metadata || {})['maputnik:openmaptiles_access_token'] - }) + static getDerivedStateFromProps(props, state) { + return { + preview: !!(props.mapStyle.metadata || {})['maputnik:openmaptiles_access_token'] + }; } onSave() { From 409f81f0d809a84bd9418c2705f63669c0f245f4 Mon Sep 17 00:00:00 2001 From: orangemug Date: Tue, 7 Aug 2018 20:13:45 +0100 Subject: [PATCH 2/8] More componentWillUpdate transition fixes. --- src/components/fields/_ZoomProperty.jsx | 13 +++++++++---- src/components/map/MapboxGlMap.jsx | 10 ++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/components/fields/_ZoomProperty.jsx b/src/components/fields/_ZoomProperty.jsx index 2d6baac..6007cae 100644 --- a/src/components/fields/_ZoomProperty.jsx +++ b/src/components/fields/_ZoomProperty.jsx @@ -62,18 +62,23 @@ export default class ZoomProperty extends React.Component { } componentDidMount() { - this.setState({ - refs: setStopRefs(this.props, this.state) - }) + const newRefs = setStopRefs(this.props, this.state); + + if(newRefs) { + this.setState({ + refs: newRefs + }) + } } - static getDerivedStateFromProps(nextProps) { + static getDerivedStateFromProps(props, state) { const newRefs = setStopRefs(props, state); if(newRefs) { return { refs: newRefs }; } + return null; } // Order the stops altering the refs to reflect their new position. diff --git a/src/components/map/MapboxGlMap.jsx b/src/components/map/MapboxGlMap.jsx index 18f3e95..1858e46 100644 --- a/src/components/map/MapboxGlMap.jsx +++ b/src/components/map/MapboxGlMap.jsx @@ -81,21 +81,23 @@ export default class MapboxGlMap extends React.Component { } } - UNSAFE_componentWillReceiveProps(nextProps) { + updateMapFromProps(props) { if(!this.state.map) return - const metadata = nextProps.mapStyle.metadata || {} + const metadata = props.mapStyle.metadata || {} MapboxGl.accessToken = metadata['maputnik:mapbox_access_token'] || tokens.mapbox - if(!nextProps.inspectModeEnabled) { + if(!props.inspectModeEnabled) { //Mapbox GL now does diffing natively so we don't need to calculate //the necessary operations ourselves! - this.state.map.setStyle(nextProps.mapStyle, { diff: true}) + this.state.map.setStyle(props.mapStyle, { diff: true}) } } componentDidUpdate(prevProps) { const map = this.state.map; + this.updateMapFromProps(this.props); + if(this.props.inspectModeEnabled !== prevProps.inspectModeEnabled) { this.state.inspect.toggleInspector() } From b5fc315b3735f805cb2e590becc6339d2ae9ef04 Mon Sep 17 00:00:00 2001 From: orangemug Date: Mon, 10 Sep 2018 14:51:22 +0100 Subject: [PATCH 3/8] setImmediate -> process.nextTick --- test/functional/util/webdriverio-ext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/util/webdriverio-ext.js b/test/functional/util/webdriverio-ext.js index ee35b48..866b775 100644 --- a/test/functional/util/webdriverio-ext.js +++ b/test/functional/util/webdriverio-ext.js @@ -46,9 +46,9 @@ try { browser.addCommand('flushReactUpdates', function() { browser.executeAsync(function(done) { // For any events to propogate - setImmediate(function() { + process.nextTick(function() { // For the DOM to be updated. - setImmediate(done); + process.nextTick(done); }) }) }) From b314642586814bca0fa90846175cf4b90c94d8f5 Mon Sep 17 00:00:00 2001 From: orangemug Date: Mon, 10 Sep 2018 14:55:45 +0100 Subject: [PATCH 4/8] Rollback to setImmediate --- test/functional/util/webdriverio-ext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/util/webdriverio-ext.js b/test/functional/util/webdriverio-ext.js index 866b775..ee35b48 100644 --- a/test/functional/util/webdriverio-ext.js +++ b/test/functional/util/webdriverio-ext.js @@ -46,9 +46,9 @@ try { browser.addCommand('flushReactUpdates', function() { browser.executeAsync(function(done) { // For any events to propogate - process.nextTick(function() { + setImmediate(function() { // For the DOM to be updated. - process.nextTick(done); + setImmediate(done); }) }) }) From be954143c3c22ec5909995b1c7c04ee475af9352 Mon Sep 17 00:00:00 2001 From: orangemug Date: Mon, 10 Sep 2018 15:09:34 +0100 Subject: [PATCH 5/8] Switch to setTimeout(fn, 0) --- test/functional/util/webdriverio-ext.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/util/webdriverio-ext.js b/test/functional/util/webdriverio-ext.js index ee35b48..67f54f5 100644 --- a/test/functional/util/webdriverio-ext.js +++ b/test/functional/util/webdriverio-ext.js @@ -46,10 +46,10 @@ try { browser.addCommand('flushReactUpdates', function() { browser.executeAsync(function(done) { // For any events to propogate - setImmediate(function() { + setTimeout(function() { // For the DOM to be updated. - setImmediate(done); - }) + setTimeout(done, 0); + }, 0) }) }) From 00b22eb9027f8364d0f944991c251e12965074c3 Mon Sep 17 00:00:00 2001 From: orangemug Date: Mon, 10 Sep 2018 15:11:06 +0100 Subject: [PATCH 6/8] OpenLayers removed in previous PR. --- test/functional/modals/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/modals/index.js b/test/functional/modals/index.js index 9f26227..d0996aa 100644 --- a/test/functional/modals/index.js +++ b/test/functional/modals/index.js @@ -171,7 +171,7 @@ describe("modals", function() { assert.equal(styleObj.metadata["maputnik:openmaptiles_access_token"], apiKey); }) - it("style renderer", function() { + it.skip("style renderer", function() { var selector = wd.$("modal-settings.maputnik:renderer"); browser.selectByValue(selector, "ol3"); browser.click(wd.$("modal-settings.name")) From c5ff67b6e00bbf6302fef12e680d1422556ce84b Mon Sep 17 00:00:00 2001 From: orangemug Date: Sat, 22 Sep 2018 12:50:40 +0100 Subject: [PATCH 7/8] Appveyor nodejs -v6 +v10 --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 0b100cc..49c572b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,8 +1,8 @@ environment: matrix: - - nodejs_version: "6" - nodejs_version: "8" - nodejs_version: "9" + - nodejs_version: "10" platform: - x86 - x64 From 3d5eec897e505b6d137c442051ddb3ce8c49e084 Mon Sep 17 00:00:00 2001 From: orangemug Date: Sat, 22 Sep 2018 13:44:41 +0100 Subject: [PATCH 8/8] Try moving to 'Visual Studio 2017' image --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 49c572b..9d469f8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,3 +1,4 @@ +image: Visual Studio 2017 environment: matrix: - nodejs_version: "8"