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<
}
}
// @deprecated noop for now until removal
export function Container(p: any) {
return p.children
}
let warnContainer: () => void
let warnUrl: () => void
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(
`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) {
// This is to make sure we don't references the router object at call time
const { pathname, asPath, query } = router
return {
get query() {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
return query
},
get pathname() {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
return pathname
},
get asPath() {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
return asPath
},
back: () => {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
router.back()
},
push: (url: string, as?: string) => {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
return router.push(url, as)
},
pushTo: (href: string, as?: string) => {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
const pushRoute = as ? href : ''
const pushUrl = as || href
return router.push(pushRoute, pushUrl)
},
replace: (url: string, as?: string) => {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
return router.replace(url, as)
},
replaceTo: (href: string, as?: string) => {
warnUrl()
if (process.env.NODE_ENV !== 'production') warnUrl()
const replaceRoute = as ? href : ''
const replaceUrl = as || href
......
import React from 'react'
import App, { Container } from 'next/app'
import App from 'next/app'
export default class MyApp extends App {
// find this
......@@ -11,10 +11,6 @@ export default class MyApp extends App {
render () {
const { Component, url } = this.props
return (
<Container>
<Component url={url} />
</Container>
)
return <Component url={url} />
}
}
import App, { Container } from 'next/app'
import App from 'next/app'
import React from 'react'
import { setState } from '../shared-module'
......@@ -42,11 +42,9 @@ export default class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Layout>
<Component {...pageProps} />
</Layout>
</Container>
<Layout>
<Component {...pageProps} />
</Layout>
)
}
}
import React from 'react'
import Link from 'next/link'
import { render } from 'react-dom'
import App, { Container, AppContext } from 'next/app'
import App, { AppContext } from 'next/app'
import { renderToString } from 'react-dom/server'
class MyApp<P = {}> extends App<P & { html: string }> {
......@@ -54,9 +54,7 @@ class MyApp<P = {}> extends App<P & { html: string }> {
</Link>
</>
) : (
<Container>
<Component {...pageProps} />
</Container>
<Component {...pageProps} />
)
}
}
......
import React from 'react'
import App, { Container } from 'next/app'
import App from 'next/app'
class MyApp extends App {
static async getInitialProps ({ Component, ctx }) {
......@@ -14,12 +14,7 @@ class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Component {...pageProps} />
</Container>
)
return <Component {...pageProps} />
}
}
......
import App, { Container } from 'next/app'
import App from 'next/app'
import React from 'react'
import '../styles.css'
......@@ -6,10 +6,6 @@ import '../styles.css'
export default class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Component {...pageProps} />
</Container>
)
return <Component {...pageProps} />
}
}
import App, { Container } from 'next/app'
import App from 'next/app'
import React from 'react'
import HeaderNav from '../components/header-nav'
......@@ -16,10 +16,10 @@ export default class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Container>
<>
<HeaderNav />
<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