Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby-react-router-scroll): Remove legacy context #21626

Merged
merged 9 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby-react-router-scroll/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"peerDependencies": {
"@reach/router": "^1.0",
"gatsby": "^2.0.0",
"react": "^0.14.0 || ^15.0.0 || ^16.0.0",
"react-dom": "^0.14.0 || ^15.0.0 || ^16.0.0"
"react": "^16.3.0",
"react-dom": "^16.3.0"
},
"repository": {
"type": "git",
Expand Down
19 changes: 7 additions & 12 deletions packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { globalHistory as history } from "@reach/router/lib/history"

import SessionStorage from "./StateStorage"

export const ScrollBehaviorContext = React.createContext()

const propTypes = {
shouldUpdateScroll: PropTypes.func,
children: PropTypes.element.isRequired,
location: PropTypes.object.isRequired,
}

const childContextTypes = {
scrollBehavior: PropTypes.object.isRequired,
}

class ScrollContext extends React.Component {
constructor(props, context) {
super(props, context)
Expand All @@ -27,12 +25,6 @@ class ScrollContext extends React.Component {
})
}

getChildContext() {
return {
scrollBehavior: this,
}
}

componentDidUpdate(prevProps) {
const { location } = this.props
const prevLocation = prevProps.location
Expand Down Expand Up @@ -95,11 +87,14 @@ class ScrollContext extends React.Component {
}

render() {
return React.Children.only(this.props.children)
return (
<ScrollBehaviorContext.Provider value={this}>
{React.Children.only(this.props.children)}
</ScrollBehaviorContext.Provider>
)
}
}

ScrollContext.propTypes = propTypes
ScrollContext.childContextTypes = childContextTypes

export default ScrollContext
29 changes: 14 additions & 15 deletions packages/gatsby-react-router-scroll/src/ScrollContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ import React from "react"
import ReactDOM from "react-dom"
import warning from "warning"
import PropTypes from "prop-types"
import { ScrollBehaviorContext } from "./ScrollBehaviorContext"

const propTypes = {
scrollKey: PropTypes.string.isRequired,
shouldUpdateScroll: PropTypes.func,
children: PropTypes.element.isRequired,
}

const contextTypes = {
// This is necessary when rendering on the client. However, when rendering on
// the server, this container will do nothing, and thus does not require the
// scroll behavior context.
scrollBehavior: PropTypes.object,
}

class ScrollContainer extends React.Component {
constructor(props, context) {
super(props, context)
constructor(props) {
super(props)

// We don't re-register if the scroll key changes, so make sure we
// unregister with the initial scroll key just in case the user changes it.
this.scrollKey = props.scrollKey
}

componentDidMount() {
this.context.scrollBehavior.registerElement(
this.props.context.registerElement(
this.props.scrollKey,
ReactDOM.findDOMNode(this), // eslint-disable-line react/no-find-dom-node
this.shouldUpdateScroll
Expand Down Expand Up @@ -56,7 +50,7 @@ class ScrollContainer extends React.Component {
}

componentWillUnmount() {
this.context.scrollBehavior.unregisterElement(this.scrollKey)
this.props.context.unregisterElement(this.scrollKey)
}

shouldUpdateScroll = (prevRouterProps, routerProps) => {
Expand All @@ -67,7 +61,7 @@ class ScrollContainer extends React.Component {

// Hack to allow accessing scrollBehavior._stateStorage.
return shouldUpdateScroll.call(
this.context.scrollBehavior.scrollBehavior,
this.props.context.scrollBehavior,
prevRouterProps,
routerProps
)
Expand All @@ -78,7 +72,12 @@ class ScrollContainer extends React.Component {
}
}

ScrollContainer.propTypes = propTypes
ScrollContainer.contextTypes = contextTypes
const ScrollContainerConsumer = props => (
<ScrollBehaviorContext.Consumer>
{context => <ScrollContainer {...props} context={context} />}
</ScrollBehaviorContext.Consumer>
)
blainekasten marked this conversation as resolved.
Show resolved Hide resolved

ScrollContainerConsumer.propTypes = propTypes

export default ScrollContainer
export default ScrollContainerConsumer