Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove UNSAFE_componentWillReceiveProps #358

Merged
merged 9 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
image: Visual Studio 2017
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "9"
- nodejs_version: "10"
platform:
- x86
- x64
Expand Down
60 changes: 33 additions & 27 deletions src/components/fields/_ZoomProperty.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,41 +58,23 @@ export default class ZoomProperty extends React.Component {
}

componentDidMount() {
this.setState({
refs: this.setStopRefs(this.props)
})
}
const newRefs = 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-");
}
if(newRefs) {
this.setState({
refs: newRefs
})
}

return newRefs;
}

UNSAFE_componentWillReceiveProps(nextProps) {
const newRefs = this.setStopRefs(nextProps);
static getDerivedStateFromProps(props, state) {
const newRefs = setStopRefs(props, state);
if(newRefs) {
this.setState({
return {
refs: newRefs
})
};
}
return null;
}

// Order the stops altering the refs to reflect their new position.
Expand Down
6 changes: 4 additions & 2 deletions src/components/inputs/NumberInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/inputs/StringInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/components/layers/JSONEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/layers/LayerEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,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 () {
Expand Down
10 changes: 6 additions & 4 deletions src/components/map/MapboxGlMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ export default class MapboxGlMap extends React.Component {
}
}

UNSAFE_componentWillReceiveProps(nextProps) {
updateMapFromProps(props) {
if(!IS_SUPPORTED) return;

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})
}
}

Expand All @@ -102,6 +102,8 @@ export default class MapboxGlMap extends React.Component {

const map = this.state.map;

this.updateMapFromProps(this.props);

if(this.props.inspectModeEnabled !== prevProps.inspectModeEnabled) {
this.state.inspect.toggleInspector()
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/map/OpenLayers3Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,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)
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/functional/modals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
6 changes: 3 additions & 3 deletions test/functional/util/webdriverio-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down