Unverified Commit f3f939dd authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Run method sorting codemod (#4909)

react-codemod recommended way of having methods defined
parent 80fdfec0
...@@ -5,6 +5,12 @@ import { execOnce, warn, loadGetInitialProps } from './utils' ...@@ -5,6 +5,12 @@ import { execOnce, warn, loadGetInitialProps } from './utils'
import { makePublicRouterInstance } from './router' import { makePublicRouterInstance } from './router'
export default class App extends Component { export default class App extends Component {
static childContextTypes = {
_containerProps: PropTypes.any,
headManager: PropTypes.object,
router: PropTypes.object
}
static displayName = 'App' static displayName = 'App'
static async getInitialProps ({ Component, router, ctx }) { static async getInitialProps ({ Component, router, ctx }) {
...@@ -12,12 +18,6 @@ export default class App extends Component { ...@@ -12,12 +18,6 @@ export default class App extends Component {
return {pageProps} return {pageProps}
} }
static childContextTypes = {
_containerProps: PropTypes.any,
headManager: PropTypes.object,
router: PropTypes.object
}
getChildContext () { getChildContext () {
const { headManager } = this.props const { headManager } = this.props
return { return {
...@@ -51,6 +51,11 @@ export class Container extends Component { ...@@ -51,6 +51,11 @@ export class Container extends Component {
this.scrollToHash() this.scrollToHash()
} }
shouldComponentUpdate (nextProps) {
// need this check not to rerender component which has already thrown an error
return !shallowEquals(this.props, nextProps)
}
componentDidUpdate () { componentDidUpdate () {
this.scrollToHash() this.scrollToHash()
} }
...@@ -67,11 +72,6 @@ export class Container extends Component { ...@@ -67,11 +72,6 @@ export class Container extends Component {
setTimeout(() => el.scrollIntoView(), 0) setTimeout(() => el.scrollIntoView(), 0)
} }
shouldComponentUpdate (nextProps) {
// need this check not to rerender component which has already thrown an error
return !shallowEquals(this.props, nextProps)
}
render () { render () {
const {children} = this.props const {children} = this.props
return <>{children}</> return <>{children}</>
......
...@@ -19,11 +19,6 @@ function isLocal (href) { ...@@ -19,11 +19,6 @@ function isLocal (href) {
const warnLink = execOnce(warn) const warnLink = execOnce(warn)
class Link extends Component { class Link extends Component {
constructor (props, ...rest) {
super(props, ...rest)
this.formatUrls(props)
}
static propTypes = exact({ static propTypes = exact({
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired, href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
...@@ -46,11 +41,37 @@ class Link extends Component { ...@@ -46,11 +41,37 @@ class Link extends Component {
]).isRequired ]).isRequired
}) })
constructor (props, ...rest) {
super(props, ...rest)
this.formatUrls(props)
}
componentDidMount () {
this.prefetch()
}
componentDidUpdate (prevProps) {
if (JSON.stringify(this.props.href) !== JSON.stringify(prevProps.href)) {
this.prefetch()
}
}
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps (nextProps) { UNSAFE_componentWillReceiveProps (nextProps) {
this.formatUrls(nextProps) this.formatUrls(nextProps)
} }
// We accept both 'href' and 'as' as objects which we can pass to `url.format`.
// We'll handle it here.
formatUrls (props) {
this.href = props.href && typeof props.href === 'object'
? format(props.href)
: props.href
this.as = props.as && typeof props.as === 'object'
? format(props.as)
: props.as
}
linkClicked = e => { linkClicked = e => {
const { nodeName, target } = e.currentTarget const { nodeName, target } = e.currentTarget
if (nodeName === 'A' && if (nodeName === 'A' &&
...@@ -107,27 +128,6 @@ class Link extends Component { ...@@ -107,27 +128,6 @@ class Link extends Component {
Router.prefetch(href) Router.prefetch(href)
} }
componentDidMount () {
this.prefetch()
}
componentDidUpdate (prevProps) {
if (JSON.stringify(this.props.href) !== JSON.stringify(prevProps.href)) {
this.prefetch()
}
}
// We accept both 'href' and 'as' as objects which we can pass to `url.format`.
// We'll handle it here.
formatUrls (props) {
this.href = props.href && typeof props.href === 'object'
? format(props.href)
: props.href
this.as = props.as && typeof props.as === 'object'
? format(props.as)
: props.as
}
render () { render () {
let { children } = this.props let { children } = this.props
let { href, as } = this let { href, as } = this
......
...@@ -34,13 +34,13 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -34,13 +34,13 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
} }
class SideEffect extends Component { class SideEffect extends Component {
// Try to use displayName of wrapped component // Expose canUseDOM so tests can monkeypatch it
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})` static canUseDOM = typeof window !== 'undefined'
static contextTypes = WrappedComponent.contextTypes static contextTypes = WrappedComponent.contextTypes
// Expose canUseDOM so tests can monkeypatch it // Try to use displayName of wrapped component
static canUseDOM = typeof window !== 'undefined' static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
static peek () { static peek () {
return state return state
...@@ -57,12 +57,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -57,12 +57,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
return recordedState return recordedState
} }
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
mountedInstances.add(this)
emitChange(this)
}
componentDidUpdate () { componentDidUpdate () {
emitChange(this) emitChange(this)
} }
...@@ -72,6 +66,12 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan ...@@ -72,6 +66,12 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
emitChange(this) emitChange(this)
} }
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
mountedInstances.add(this)
emitChange(this)
}
render () { render () {
return <WrappedComponent>{ this.props.children }</WrappedComponent> return <WrappedComponent>{ this.props.children }</WrappedComponent>
} }
......
...@@ -9,16 +9,16 @@ const Fragment = React.Fragment || function Fragment ({ children }) { ...@@ -9,16 +9,16 @@ const Fragment = React.Fragment || function Fragment ({ children }) {
} }
export default class Document extends Component { export default class Document extends Component {
static childContextTypes = {
_documentProps: PropTypes.any
}
static getInitialProps ({ renderPage }) { static getInitialProps ({ renderPage }) {
const { html, head, errorHtml, buildManifest } = renderPage() const { html, head, errorHtml, buildManifest } = renderPage()
const styles = flush() const styles = flush()
return { html, head, errorHtml, styles, buildManifest } return { html, head, errorHtml, styles, buildManifest }
} }
static childContextTypes = {
_documentProps: PropTypes.any
}
getChildContext () { getChildContext () {
return { _documentProps: this.props } return { _documentProps: this.props }
} }
...@@ -43,58 +43,58 @@ export class Head extends Component { ...@@ -43,58 +43,58 @@ export class Head extends Component {
nonce: PropTypes.string nonce: PropTypes.string
} }
getPreloadMainLinks () { getCssLinks () {
const { assetPrefix, files } = this.context._documentProps const { assetPrefix, files } = this.context._documentProps
if(!files || files.length === 0) { if(!files || files.length === 0) {
return null return null
} }
return files.map((file) => { return files.map((file) => {
// Only render .js files here // Only render .css files here
if(!/\.js$/.exec(file)) { if(!/\.css$/.exec(file)) {
return null return null
} }
return <link return <link
key={file} key={file}
nonce={this.props.nonce} nonce={this.props.nonce}
rel='preload' rel='stylesheet'
href={`${assetPrefix}/_next/${file}`} href={`${assetPrefix}/_next/${file}`}
/>
})
}
getPreloadDynamicChunks () {
const { dynamicImports, assetPrefix } = this.context._documentProps
return dynamicImports.map((bundle) => {
return <script
async
key={bundle.file}
src={`${assetPrefix}/_next/${bundle.file}`}
as='script' as='script'
nonce={this.props.nonce}
/> />
}) })
} }
getCssLinks () { getPreloadMainLinks () {
const { assetPrefix, files } = this.context._documentProps const { assetPrefix, files } = this.context._documentProps
if(!files || files.length === 0) { if(!files || files.length === 0) {
return null return null
} }
return files.map((file) => { return files.map((file) => {
// Only render .css files here // Only render .js files here
if(!/\.css$/.exec(file)) { if(!/\.js$/.exec(file)) {
return null return null
} }
return <link return <link
key={file} key={file}
nonce={this.props.nonce} nonce={this.props.nonce}
rel='stylesheet' rel='preload'
href={`${assetPrefix}/_next/${file}`} href={`${assetPrefix}/_next/${file}`}
/>
})
}
getPreloadDynamicChunks () {
const { dynamicImports, assetPrefix } = this.context._documentProps
return dynamicImports.map((bundle) => {
return <script
async
key={bundle.file}
src={`${assetPrefix}/_next/${bundle.file}`}
as='script' as='script'
nonce={this.props.nonce}
/> />
}) })
} }
...@@ -135,12 +135,24 @@ export class Main extends Component { ...@@ -135,12 +135,24 @@ export class Main extends Component {
} }
export class NextScript extends Component { export class NextScript extends Component {
static contextTypes = {
_documentProps: PropTypes.any
}
static propTypes = { static propTypes = {
nonce: PropTypes.string nonce: PropTypes.string
} }
static contextTypes = { getDynamicChunks () {
_documentProps: PropTypes.any const { dynamicImports, assetPrefix } = this.context._documentProps
return dynamicImports.map((bundle) => {
return <script
async
key={bundle.file}
src={`${assetPrefix}/_next/${bundle.file}`}
nonce={this.props.nonce}
/>
})
} }
getScripts () { getScripts () {
...@@ -164,18 +176,6 @@ export class NextScript extends Component { ...@@ -164,18 +176,6 @@ export class NextScript extends Component {
}) })
} }
getDynamicChunks () {
const { dynamicImports, assetPrefix } = this.context._documentProps
return dynamicImports.map((bundle) => {
return <script
async
key={bundle.file}
src={`${assetPrefix}/_next/${bundle.file}`}
nonce={this.props.nonce}
/>
})
}
render () { render () {
const { staticMarkup, assetPrefix, __NEXT_DATA__ } = this.context._documentProps const { staticMarkup, assetPrefix, __NEXT_DATA__ } = this.context._documentProps
const { page, pathname, buildId } = __NEXT_DATA__ const { page, pathname, buildId } = __NEXT_DATA__
......
...@@ -24,14 +24,14 @@ const HelloBundle = dynamic({ ...@@ -24,14 +24,14 @@ const HelloBundle = dynamic({
}) })
export default class Bundle extends React.Component { export default class Bundle extends React.Component {
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
static childContextTypes = { static childContextTypes = {
data: PropTypes.object data: PropTypes.object
} }
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
getChildContext () { getChildContext () {
return { return {
data: { title: 'ZEIT Rocks' } data: { title: 'ZEIT Rocks' }
......
...@@ -16,17 +16,17 @@ export default class extends Component { ...@@ -16,17 +16,17 @@ export default class extends Component {
return { getInitialPropsRunCount } return { getInitialPropsRunCount }
} }
getCurrentCounter () {
const { url } = this.props
return url.query.counter ? parseInt(url.query.counter) : 0
}
increase () { increase () {
const counter = this.getCurrentCounter() const counter = this.getCurrentCounter()
const href = `/nav/shallow-routing?counter=${counter + 1}` const href = `/nav/shallow-routing?counter=${counter + 1}`
Router.push(href, href, { shallow: true }) Router.push(href, href, { shallow: true })
} }
getCurrentCounter () {
const { url } = this.props
return url.query.counter ? parseInt(url.query.counter) : 0
}
render () { render () {
return ( return (
<div className='shallow-routing'> <div className='shallow-routing'>
......
...@@ -23,14 +23,14 @@ const HelloBundle = dynamic({ ...@@ -23,14 +23,14 @@ const HelloBundle = dynamic({
}) })
export default class Bundle extends React.Component { export default class Bundle extends React.Component {
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
static childContextTypes = { static childContextTypes = {
data: PropTypes.object data: PropTypes.object
} }
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
getChildContext () { getChildContext () {
return { return {
data: { title: 'ZEIT Rocks' } data: { title: 'ZEIT Rocks' }
......
...@@ -3,12 +3,12 @@ import React from 'react' ...@@ -3,12 +3,12 @@ import React from 'react'
import Link from 'next/link' import Link from 'next/link'
export default class DynamicPage extends React.Component { export default class DynamicPage extends React.Component {
state = {}
static getInitialProps ({ query }) { static getInitialProps ({ query }) {
return { text: query.text } return { text: query.text }
} }
state = {}
componentDidMount () { componentDidMount () {
const [, hash] = location.href.split('#') const [, hash] = location.href.split('#')
this.setState({ hash }) this.setState({ hash })
......
...@@ -30,9 +30,9 @@ class HeaderNav extends React.Component { ...@@ -30,9 +30,9 @@ class HeaderNav extends React.Component {
this.props.router.events.off('routeChangeComplete', this.handleRouteChange) this.props.router.events.off('routeChangeComplete', this.handleRouteChange)
} }
handleRouteChangeTopLevelRouterDeprecatedBehavior = url => { handleRouteChange = url => {
this.setState({ this.setState({
activeURLTopLevelRouterDeprecatedBehavior: url activeURL: url
}) })
}; };
...@@ -42,9 +42,9 @@ class HeaderNav extends React.Component { ...@@ -42,9 +42,9 @@ class HeaderNav extends React.Component {
}) })
}; };
handleRouteChange = url => { handleRouteChangeTopLevelRouterDeprecatedBehavior = url => {
this.setState({ this.setState({
activeURL: url activeURLTopLevelRouterDeprecatedBehavior: url
}) })
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment