From b20b80adf099db669c55936fe092a8d4bd9d7d39 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 6 Aug 2018 20:23:28 -0700 Subject: [PATCH] Run rename-unsafe-lifecycles as preparation for React 17 (#4907) https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles --- lib/link.js | 28 +++++++++++-------- lib/side-effect.js | 7 ++++- package.json | 1 + .../basic/pages/nav/url-prop-change.js | 3 +- test/integration/basic/pages/stateful.js | 3 +- yarn.lock | 4 +++ 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/link.js b/lib/link.js index a7b9a88a..76e968e5 100644 --- a/lib/link.js +++ b/lib/link.js @@ -2,12 +2,23 @@ import { resolve, format, parse } from 'url' import React, { Component, Children } from 'react' +import {polyfill} from 'react-lifecycles-compat' import PropTypes from 'prop-types' import exact from 'prop-types-exact' import Router, { _rewriteUrlForNextExport } from './router' import { warn, execOnce, getLocationOrigin } from './utils' -export default class Link extends Component { +function isLocal (href) { + const url = parse(href, false, true) + const origin = parse(getLocationOrigin(), false, true) + + return !url.host || + (url.protocol === origin.protocol && url.host === origin.host) +} + +const warnLink = execOnce(warn) + +class Link extends Component { constructor (props, ...rest) { super(props, ...rest) this.formatUrls(props) @@ -35,7 +46,8 @@ export default class Link extends Component { ]).isRequired }) - componentWillReceiveProps (nextProps) { + // eslint-disable-next-line camelcase + UNSAFE_componentWillReceiveProps (nextProps) { this.formatUrls(nextProps) } @@ -157,12 +169,6 @@ export default class Link extends Component { } } -function isLocal (href) { - const url = parse(href, false, true) - const origin = parse(getLocationOrigin(), false, true) - - return !url.host || - (url.protocol === origin.protocol && url.host === origin.host) -} - -const warnLink = execOnce(warn) +// Make UNSAFE_ compatible with version of React under 16.3 +polyfill(Link) +export default Link diff --git a/lib/side-effect.js b/lib/side-effect.js index 954e4e68..d399274b 100644 --- a/lib/side-effect.js +++ b/lib/side-effect.js @@ -1,4 +1,5 @@ import React, { Component } from 'react' +import {polyfill} from 'react-lifecycles-compat' import { getDisplayName } from './utils' export default function withSideEffect (reduceComponentsToState, handleStateChangeOnClient, mapStateOnServer) { @@ -56,7 +57,8 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan return recordedState } - componentWillMount () { + // eslint-disable-next-line camelcase + UNSAFE_componentWillMount () { mountedInstances.add(this) emitChange(this) } @@ -75,6 +77,9 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan } } + // Make UNSAFE_ compatible with version of React under 16.3 + polyfill(SideEffect) + return SideEffect } } diff --git a/package.json b/package.json index 5e44a11a..caf710cc 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "prop-types": "15.6.0", "prop-types-exact": "1.1.1", "react-error-overlay": "4.0.0", + "react-lifecycles-compat": "3.0.4", "react-loadable": "5.4.0", "recursive-copy": "2.0.6", "resolve": "1.5.0", diff --git a/test/integration/basic/pages/nav/url-prop-change.js b/test/integration/basic/pages/nav/url-prop-change.js index 79715a08..9d4cc67c 100644 --- a/test/integration/basic/pages/nav/url-prop-change.js +++ b/test/integration/basic/pages/nav/url-prop-change.js @@ -10,7 +10,8 @@ export default class UrlPropChange extends React.Component { } } - componentWillReceiveProps (nextProps) { + // eslint-disable-next-line camelcase + UNSAFE_componentWillReceiveProps (nextProps) { this.setState(() => { return { previousUrl: this.props.url, diff --git a/test/integration/basic/pages/stateful.js b/test/integration/basic/pages/stateful.js index aee5c69e..76d2543b 100644 --- a/test/integration/basic/pages/stateful.js +++ b/test/integration/basic/pages/stateful.js @@ -8,7 +8,8 @@ export default class Statefull extends Component { this.state = { answer: null } } - componentWillMount () { + // eslint-disable-next-line camelcase + UNSAFE_componentWillMount () { this.setState({ answer: 42 }) } diff --git a/yarn.lock b/yarn.lock index 3c58b7f7..da3b1790 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6553,6 +6553,10 @@ react-error-overlay@4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-4.0.0.tgz#d198408a85b4070937a98667f500c832f86bd5d4" +react-lifecycles-compat@3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362" + react-loadable@5.4.0: version "5.4.0" resolved "https://registry.yarnpkg.com/react-loadable/-/react-loadable-5.4.0.tgz#3b6b7d51121a7868fd155be848a36e02084742c9"