Skip to content

Commit

Permalink
Migrate next/head to use React.createContext (#6038)
Browse files Browse the repository at this point in the history
Continuation of #5945
  • Loading branch information
alexandernanberg authored and timneutkens committed Jan 14, 2019
1 parent 02ab732 commit ff5cf6d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
3 changes: 3 additions & 0 deletions packages/next-server/lib/head-manager-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react'

export const HeadManagerContext = React.createContext(null)
9 changes: 4 additions & 5 deletions packages/next-server/lib/head.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react'
import PropTypes from 'prop-types'
import sideEffect from './side-effect'
import { HeadManagerContext } from './head-manager-context'

class Head extends React.Component {
static contextTypes = {
headManager: PropTypes.object
}
static contextType = HeadManagerContext

render () {
return null
Expand Down Expand Up @@ -47,8 +46,8 @@ function mapOnServer (head) {
}

function onStateChange (head) {
if (this.context && this.context.headManager) {
this.context.headManager.updateHead(head)
if (this.context) {
this.context.updateHead(head)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next-server/lib/side-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
// Expose canUseDOM so tests can monkeypatch it
static canUseDOM = typeof window !== 'undefined'

static contextTypes = WrappedComponent.contextTypes
static contextType = WrappedComponent.contextType

// Try to use displayName of wrapped component
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
Expand Down
4 changes: 2 additions & 2 deletions packages/next-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"url": "0.11.0"
},
"peerDependencies": {
"react": "^16.3.0",
"react-dom": "^16.3.0"
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"devDependencies": {
"@taskr/clear": "1.1.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/next/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PageLoader from './page-loader'
import * as envConfig from 'next-server/config'
import ErrorBoundary from './error-boundary'
import Loadable from 'next-server/dist/lib/loadable'
import { HeadManagerContext } from 'next-server/dist/lib/head-manager-context'

// Polyfill Promise globally
// This is needed because Webpack's dynamic loading(common chunks) code
Expand Down Expand Up @@ -181,7 +182,9 @@ async function doRender ({ App, Component, props, err, emitter: emitterProp = em
if (process.env.NODE_ENV === 'development') {
renderReactElement((
<RouterContext.Provider value={makePublicRouterInstance(router)}>
<App {...appProps} />
<HeadManagerContext.Provider value={headManager}>
<App {...appProps} />
</HeadManagerContext.Provider>
</RouterContext.Provider>
), appContainer)
} else {
Expand All @@ -196,7 +199,9 @@ async function doRender ({ App, Component, props, err, emitter: emitterProp = em
renderReactElement((
<ErrorBoundary onError={onError}>
<RouterContext.Provider value={makePublicRouterInstance(router)}>
<App {...appProps} />
<HeadManagerContext.Provider value={headManager}>
<App {...appProps} />
</HeadManagerContext.Provider>
</RouterContext.Provider>
</ErrorBoundary>
), appContainer)
Expand Down
4 changes: 2 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
"ws": "6.1.2"
},
"peerDependencies": {
"react": "^16.3.0",
"react-dom": "^16.3.0"
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"devDependencies": {
"@types/loader-utils": "1.1.3",
Expand Down
20 changes: 5 additions & 15 deletions packages/next/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { execOnce, loadGetInitialProps } from 'next-server/dist/lib/utils'

export default class App extends Component {
static childContextTypes = {
headManager: PropTypes.object
}

static async getInitialProps ({ Component, router, ctx }) {
const pageProps = await loadGetInitialProps(Component, ctx)
return {pageProps}
}

getChildContext () {
const { headManager } = this.props
return {
headManager
}
}

// Kept here for backwards compatibility.
// When someone ended App they could call `super.componentDidCatch`. This is now deprecated.
componentDidCatch (err) {
Expand All @@ -28,9 +16,11 @@ export default class App extends Component {
render () {
const {router, Component, pageProps} = this.props
const url = createUrl(router)
return <Container>
<Component {...pageProps} url={url} />
</Container>
return (
<Container>
<Component {...pageProps} url={url} />
</Container>
)
}
}

Expand Down

0 comments on commit ff5cf6d

Please sign in to comment.