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'
import { makePublicRouterInstance } from './router'
export default class App extends Component {
static childContextTypes = {
_containerProps: PropTypes.any,
headManager: PropTypes.object,
router: PropTypes.object
}
static displayName = 'App'
static async getInitialProps ({ Component, router, ctx }) {
......@@ -12,12 +18,6 @@ export default class App extends Component {
return {pageProps}
}
static childContextTypes = {
_containerProps: PropTypes.any,
headManager: PropTypes.object,
router: PropTypes.object
}
getChildContext () {
const { headManager } = this.props
return {
......@@ -51,6 +51,11 @@ export class Container extends Component {
this.scrollToHash()
}
shouldComponentUpdate (nextProps) {
// need this check not to rerender component which has already thrown an error
return !shallowEquals(this.props, nextProps)
}
componentDidUpdate () {
this.scrollToHash()
}
......@@ -67,11 +72,6 @@ export class Container extends Component {
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 () {
const {children} = this.props
return <>{children}</>
......
......@@ -19,11 +19,6 @@ function isLocal (href) {
const warnLink = execOnce(warn)
class Link extends Component {
constructor (props, ...rest) {
super(props, ...rest)
this.formatUrls(props)
}
static propTypes = exact({
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
......@@ -46,11 +41,37 @@ class Link extends Component {
]).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
UNSAFE_componentWillReceiveProps (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 => {
const { nodeName, target } = e.currentTarget
if (nodeName === 'A' &&
......@@ -107,27 +128,6 @@ class Link extends Component {
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 () {
let { children } = this.props
let { href, as } = this
......
......@@ -34,13 +34,13 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
}
class SideEffect extends Component {
// Try to use displayName of wrapped component
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
// Expose canUseDOM so tests can monkeypatch it
static canUseDOM = typeof window !== 'undefined'
static contextTypes = WrappedComponent.contextTypes
// Expose canUseDOM so tests can monkeypatch it
static canUseDOM = typeof window !== 'undefined'
// Try to use displayName of wrapped component
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
static peek () {
return state
......@@ -57,12 +57,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
return recordedState
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
mountedInstances.add(this)
emitChange(this)
}
componentDidUpdate () {
emitChange(this)
}
......@@ -72,6 +66,12 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
emitChange(this)
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillMount () {
mountedInstances.add(this)
emitChange(this)
}
render () {
return <WrappedComponent>{ this.props.children }</WrappedComponent>
}
......
......@@ -9,16 +9,16 @@ const Fragment = React.Fragment || function Fragment ({ children }) {
}
export default class Document extends Component {
static childContextTypes = {
_documentProps: PropTypes.any
}
static getInitialProps ({ renderPage }) {
const { html, head, errorHtml, buildManifest } = renderPage()
const styles = flush()
return { html, head, errorHtml, styles, buildManifest }
}
static childContextTypes = {
_documentProps: PropTypes.any
}
getChildContext () {
return { _documentProps: this.props }
}
......@@ -43,58 +43,58 @@ export class Head extends Component {
nonce: PropTypes.string
}
getPreloadMainLinks () {
getCssLinks () {
const { assetPrefix, files } = this.context._documentProps
if(!files || files.length === 0) {
return null
}
return files.map((file) => {
// Only render .js files here
if(!/\.js$/.exec(file)) {
// Only render .css files here
if(!/\.css$/.exec(file)) {
return null
}
return <link
key={file}
nonce={this.props.nonce}
rel='preload'
rel='stylesheet'
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'
nonce={this.props.nonce}
/>
})
}
getCssLinks () {
getPreloadMainLinks () {
const { assetPrefix, files } = this.context._documentProps
if(!files || files.length === 0) {
return null
}
return files.map((file) => {
// Only render .css files here
if(!/\.css$/.exec(file)) {
// Only render .js files here
if(!/\.js$/.exec(file)) {
return null
}
return <link
key={file}
nonce={this.props.nonce}
rel='stylesheet'
rel='preload'
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'
nonce={this.props.nonce}
/>
})
}
......@@ -135,12 +135,24 @@ export class Main extends Component {
}
export class NextScript extends Component {
static contextTypes = {
_documentProps: PropTypes.any
}
static propTypes = {
nonce: PropTypes.string
}
static contextTypes = {
_documentProps: PropTypes.any
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}
/>
})
}
getScripts () {
......@@ -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 () {
const { staticMarkup, assetPrefix, __NEXT_DATA__ } = this.context._documentProps
const { page, pathname, buildId } = __NEXT_DATA__
......
......@@ -24,14 +24,14 @@ const HelloBundle = dynamic({
})
export default class Bundle extends React.Component {
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
static childContextTypes = {
data: PropTypes.object
}
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
getChildContext () {
return {
data: { title: 'ZEIT Rocks' }
......
......@@ -16,17 +16,17 @@ export default class extends Component {
return { getInitialPropsRunCount }
}
getCurrentCounter () {
const { url } = this.props
return url.query.counter ? parseInt(url.query.counter) : 0
}
increase () {
const counter = this.getCurrentCounter()
const href = `/nav/shallow-routing?counter=${counter + 1}`
Router.push(href, href, { shallow: true })
}
getCurrentCounter () {
const { url } = this.props
return url.query.counter ? parseInt(url.query.counter) : 0
}
render () {
return (
<div className='shallow-routing'>
......
......@@ -23,14 +23,14 @@ const HelloBundle = dynamic({
})
export default class Bundle extends React.Component {
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
static childContextTypes = {
data: PropTypes.object
}
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
getChildContext () {
return {
data: { title: 'ZEIT Rocks' }
......
......@@ -3,12 +3,12 @@ import React from 'react'
import Link from 'next/link'
export default class DynamicPage extends React.Component {
state = {}
static getInitialProps ({ query }) {
return { text: query.text }
}
state = {}
componentDidMount () {
const [, hash] = location.href.split('#')
this.setState({ hash })
......
......@@ -30,9 +30,9 @@ class HeaderNav extends React.Component {
this.props.router.events.off('routeChangeComplete', this.handleRouteChange)
}
handleRouteChangeTopLevelRouterDeprecatedBehavior = url => {
handleRouteChange = url => {
this.setState({
activeURLTopLevelRouterDeprecatedBehavior: url
activeURL: url
})
};
......@@ -42,9 +42,9 @@ class HeaderNav extends React.Component {
})
};
handleRouteChange = url => {
handleRouteChangeTopLevelRouterDeprecatedBehavior = url => {
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