Skip to content

Commit

Permalink
fix: Context Provider could crash update, #944
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed May 18, 2018
1 parent ca047a3 commit b0e2b5b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
22 changes: 22 additions & 0 deletions examples/all-possible-containers/src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @flow
import React from 'react'

import Context from '../context'
import Counter from './Counter'

import ErrorBoundary from './ErrorBoundary'
import ModalComponent from './ModalComponent'

Expand All @@ -16,6 +19,16 @@ import ConnectedChildrenAFComponent from './ConnectedChildrenAFComponent'
import FunctionConsumerPureClassComponent from './FunctionConsumerPureClassComponent'
import { EDIT_ME } from './_editMe'

const Secret = (() => {
const A = () => (
<div>
component A <Counter />
</div>
)
const B = () => 'wrong'
return { A, B }
})()

class App extends React.Component {
state = {
error: null,
Expand All @@ -32,6 +45,7 @@ class App extends React.Component {

render() {
const { open, error, errorInfo } = this.state
const { A, B } = Secret

return error ? (
<ErrorBoundary error={error} errorInfo={errorInfo} />
Expand Down Expand Up @@ -60,6 +74,14 @@ class App extends React.Component {
onRequestClose={() => this.setState({ open: false })}
/>
)}
<div>
<Context.Provider value="42">
<Context.Consumer>
{value => (value === '42' ? <A /> : <B />)}
</Context.Consumer>
</Context.Provider>
<PureClassComponent />
</div>
</React.Fragment>
</div>
)
Expand Down
22 changes: 22 additions & 0 deletions examples/all-possible-containers/src/components/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react'

class Counter extends React.Component {
state = { count: 0 }

componentDidMount() {
this.interval = setInterval(
() => this.setState(prevState => ({ count: prevState.count + 1 })),
200,
)
}

componentWillUnmount() {
clearInterval(this.interval)
}

render() {
return <div>#{this.state.count}</div>
}
}

export default Counter
8 changes: 7 additions & 1 deletion src/reconciler/hotReplacementRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ const hotReplacementRender = (instance, stack) => {

if (isContextProvider(child)) {
extraContext = new Map(extraContext)
extraContext.set(getContextProvider(child.type), child.props.value)
extraContext.set(
getContextProvider(child.type),
{
...(child.nextProps || {}),
...(child.props || {}),
}.value,
)
childName = 'ContextProvider'
}

Expand Down

0 comments on commit b0e2b5b

Please sign in to comment.