Commit bf114219 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add deprecation warning for App Container (#8475)

* Add deprecation warning for App Container

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Tweak for more dead-code elimination
parent be850d03
# App Container Deprecated
#### Why This Error Occurred
In versions prior to v9.0.4 the `<Container>` component was used in `./pages/_app.js` to handle scrolling to hashes.
This handling has been moved up the tree so the `<Container>` component is no longer needed in your custom `<App>`!
#### Possible Ways to Fix It
Remove the `<Container>` component from your Custom `<App>` (`./pages/_app.js`).
**Before**
```jsx
import React from 'react'
import App, { Container } from 'next/app'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return (
<Container>
<Component {...pageProps} />
</Container>
)
}
}
export default MyApp
```
**After**
```jsx
import React from 'react'
import App from 'next/app'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}
export default MyApp
```
...@@ -57,56 +57,66 @@ export default class App<P = {}, CP = {}, S = {}> extends React.Component< ...@@ -57,56 +57,66 @@ export default class App<P = {}, CP = {}, S = {}> extends React.Component<
} }
} }
// @deprecated noop for now until removal let warnContainer: () => void
export function Container(p: any) { let warnUrl: () => void
return p.children
}
const warnUrl = execOnce(() => { if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production') { warnContainer = execOnce(() => {
console.warn(
`Warning: the \`Container\` in \`_app\` has been deprecated and should be removed. https://err.sh/zeit/next.js/app-container-deprecated`
)
})
warnUrl = execOnce(() => {
console.error( console.error(
`Warning: the 'url' property is deprecated. https://err.sh/zeit/next.js/url-deprecated` `Warning: the 'url' property is deprecated. https://err.sh/zeit/next.js/url-deprecated`
) )
} })
}) }
// @deprecated noop for now until removal
export function Container(p: any) {
if (process.env.NODE_ENV !== 'production') warnContainer()
return p.children
}
export function createUrl(router: Router) { export function createUrl(router: Router) {
// This is to make sure we don't references the router object at call time // This is to make sure we don't references the router object at call time
const { pathname, asPath, query } = router const { pathname, asPath, query } = router
return { return {
get query() { get query() {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
return query return query
}, },
get pathname() { get pathname() {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
return pathname return pathname
}, },
get asPath() { get asPath() {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
return asPath return asPath
}, },
back: () => { back: () => {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
router.back() router.back()
}, },
push: (url: string, as?: string) => { push: (url: string, as?: string) => {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
return router.push(url, as) return router.push(url, as)
}, },
pushTo: (href: string, as?: string) => { pushTo: (href: string, as?: string) => {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
const pushRoute = as ? href : '' const pushRoute = as ? href : ''
const pushUrl = as || href const pushUrl = as || href
return router.push(pushRoute, pushUrl) return router.push(pushRoute, pushUrl)
}, },
replace: (url: string, as?: string) => { replace: (url: string, as?: string) => {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
return router.replace(url, as) return router.replace(url, as)
}, },
replaceTo: (href: string, as?: string) => { replaceTo: (href: string, as?: string) => {
warnUrl() if (process.env.NODE_ENV !== 'production') warnUrl()
const replaceRoute = as ? href : '' const replaceRoute = as ? href : ''
const replaceUrl = as || href const replaceUrl = as || href
......
import React from 'react' import React from 'react'
import App, { Container } from 'next/app' import App from 'next/app'
export default class MyApp extends App { export default class MyApp extends App {
// find this // find this
...@@ -11,10 +11,6 @@ export default class MyApp extends App { ...@@ -11,10 +11,6 @@ export default class MyApp extends App {
render () { render () {
const { Component, url } = this.props const { Component, url } = this.props
return ( return <Component url={url} />
<Container>
<Component url={url} />
</Container>
)
} }
} }
import App, { Container } from 'next/app' import App from 'next/app'
import React from 'react' import React from 'react'
import { setState } from '../shared-module' import { setState } from '../shared-module'
...@@ -42,11 +42,9 @@ export default class MyApp extends App { ...@@ -42,11 +42,9 @@ export default class MyApp extends App {
render () { render () {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return ( return (
<Container> <Layout>
<Layout> <Component {...pageProps} />
<Component {...pageProps} /> </Layout>
</Layout>
</Container>
) )
} }
} }
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { render } from 'react-dom' import { render } from 'react-dom'
import App, { Container, AppContext } from 'next/app' import App, { AppContext } from 'next/app'
import { renderToString } from 'react-dom/server' import { renderToString } from 'react-dom/server'
class MyApp<P = {}> extends App<P & { html: string }> { class MyApp<P = {}> extends App<P & { html: string }> {
...@@ -54,9 +54,7 @@ class MyApp<P = {}> extends App<P & { html: string }> { ...@@ -54,9 +54,7 @@ class MyApp<P = {}> extends App<P & { html: string }> {
</Link> </Link>
</> </>
) : ( ) : (
<Container> <Component {...pageProps} />
<Component {...pageProps} />
</Container>
) )
} }
} }
......
import React from 'react' import React from 'react'
import App, { Container } from 'next/app' import App from 'next/app'
class MyApp extends App { class MyApp extends App {
static async getInitialProps ({ Component, ctx }) { static async getInitialProps ({ Component, ctx }) {
...@@ -14,12 +14,7 @@ class MyApp extends App { ...@@ -14,12 +14,7 @@ class MyApp extends App {
render () { render () {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return <Component {...pageProps} />
return (
<Container>
<Component {...pageProps} />
</Container>
)
} }
} }
......
import App, { Container } from 'next/app' import App from 'next/app'
import React from 'react' import React from 'react'
import '../styles.css' import '../styles.css'
...@@ -6,10 +6,6 @@ import '../styles.css' ...@@ -6,10 +6,6 @@ import '../styles.css'
export default class MyApp extends App { export default class MyApp extends App {
render () { render () {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return ( return <Component {...pageProps} />
<Container>
<Component {...pageProps} />
</Container>
)
} }
} }
import App, { Container } from 'next/app' import App from 'next/app'
import React from 'react' import React from 'react'
import HeaderNav from '../components/header-nav' import HeaderNav from '../components/header-nav'
...@@ -16,10 +16,10 @@ export default class MyApp extends App { ...@@ -16,10 +16,10 @@ export default class MyApp extends App {
render () { render () {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return ( return (
<Container> <>
<HeaderNav /> <HeaderNav />
<Component {...pageProps} /> <Component {...pageProps} />
</Container> </>
) )
} }
} }
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