Skip to content

Commit

Permalink
Don't depend on console and don't warn in production builds
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Jan 29, 2016
1 parent 9937837 commit 208efae
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
const { Component, PropTypes, Children } = require('react')
const storeShape = require('../utils/storeShape')

let didWarnAboutReceivingStore = false
function warnAboutReceivingStore() {
if (didWarnAboutReceivingStore) {
return
if (process.env.NODE_ENV !== 'production') {
let didWarnAboutReceivingStore = false
/* eslint-disable no-var */
var warnAboutReceivingStore = function () {
/* eslint-enable no-var */
if (didWarnAboutReceivingStore) {
return
}
didWarnAboutReceivingStore = true

/* eslint-disable no-console */
if (typeof console !== 'undefined' && typeof console.error === 'function') {
console.error(
'<Provider> does not support changing `store` on the fly. ' +
'It is most likely that you see this error because you updated to ' +
'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +
'automatically. See https://github.com/rackt/react-redux/releases/' +
'tag/v2.0.0 for the migration instructions.'
)
}
/* eslint-disable no-console */
}

didWarnAboutReceivingStore = true
console.error( // eslint-disable-line no-console
'<Provider> does not support changing `store` on the fly. ' +
'It is most likely that you see this error because you updated to ' +
'Redux 2.x and React Redux 2.x which no longer hot reload reducers ' +
'automatically. See https://github.com/rackt/react-redux/releases/' +
'tag/v2.0.0 for the migration instructions.'
)
}

class Provider extends Component {
Expand All @@ -27,19 +35,21 @@ class Provider extends Component {
this.store = props.store
}

componentWillReceiveProps(nextProps) {
render() {
let { children } = this.props
return Children.only(children)
}
}

if (process.env.NODE_ENV !== 'production') {
Provider.prototype.componentWillReceiveProps = function (nextProps) {
const { store } = this
const { store: nextStore } = nextProps

if (store !== nextStore) {
warnAboutReceivingStore()
}
}

render() {
let { children } = this.props
return Children.only(children)
}
}

Provider.propTypes = {
Expand Down

0 comments on commit 208efae

Please sign in to comment.