Skip to content

Commit 9ebd1a9

Browse files
Andaristtimdorr
authored andcommitted
Remove invariant in favor of doing NODE_ENV checks directly (#1472)
1 parent 9fc5993 commit 9ebd1a9

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"dependencies": {
5151
"@babel/runtime": "^7.5.5",
5252
"hoist-non-react-statics": "^3.3.0",
53-
"invariant": "^2.2.4",
5453
"loose-envify": "^1.4.0",
5554
"prop-types": "^15.7.2",
5655
"react-is": "^16.9.0"

src/components/connectAdvanced.js

+42-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hoistStatics from 'hoist-non-react-statics'
2-
import invariant from 'invariant'
32
import React, { useContext, useMemo, useRef, useReducer } from 'react'
43
import { isValidElementType, isContextConsumer } from 'react-is'
54
import Subscription from '../utils/Subscription'
@@ -78,34 +77,40 @@ export default function connectAdvanced(
7877
...connectOptions
7978
} = {}
8079
) {
81-
invariant(
82-
renderCountProp === undefined,
83-
`renderCountProp is removed. render counting is built into the latest React Dev Tools profiling extension`
84-
)
85-
86-
invariant(
87-
!withRef,
88-
'withRef is removed. To access the wrapped instance, use a ref on the connected component'
89-
)
90-
91-
const customStoreWarningMessage =
92-
'To use a custom Redux store for specific components, create a custom React context with ' +
93-
"React.createContext(), and pass the context object to React Redux's Provider and specific components" +
94-
' like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. ' +
95-
'You may also pass a {context : MyContext} option to connect'
96-
97-
invariant(
98-
storeKey === 'store',
99-
'storeKey has been removed and does not do anything. ' +
100-
customStoreWarningMessage
101-
)
80+
if (process.env.NODE_ENV !== 'production') {
81+
if (renderCountProp !== undefined) {
82+
throw new Error(
83+
`renderCountProp is removed. render counting is built into the latest React Dev Tools profiling extension`
84+
)
85+
}
86+
if (withRef) {
87+
throw new Error(
88+
'withRef is removed. To access the wrapped instance, use a ref on the connected component'
89+
)
90+
}
91+
92+
const customStoreWarningMessage =
93+
'To use a custom Redux store for specific components, create a custom React context with ' +
94+
"React.createContext(), and pass the context object to React Redux's Provider and specific components" +
95+
' like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. ' +
96+
'You may also pass a {context : MyContext} option to connect'
97+
98+
if (storeKey !== 'store') {
99+
throw new Error(
100+
'storeKey has been removed and does not do anything. ' +
101+
customStoreWarningMessage
102+
)
103+
}
104+
}
102105

103106
const Context = context
104107

105108
return function wrapWithConnect(WrappedComponent) {
106-
if (process.env.NODE_ENV !== 'production') {
107-
invariant(
108-
isValidElementType(WrappedComponent),
109+
if (
110+
process.env.NODE_ENV !== 'production' &&
111+
!isValidElementType(WrappedComponent)
112+
) {
113+
throw new Error(
109114
`You must pass a component to the function returned by ` +
110115
`${methodName}. Instead received ${stringifyComponent(
111116
WrappedComponent
@@ -173,13 +178,18 @@ export default function connectAdvanced(
173178
const didStoreComeFromContext =
174179
Boolean(contextValue) && Boolean(contextValue.store)
175180

176-
invariant(
177-
didStoreComeFromProps || didStoreComeFromContext,
178-
`Could not find "store" in the context of ` +
179-
`"${displayName}". Either wrap the root component in a <Provider>, ` +
180-
`or pass a custom React context provider to <Provider> and the corresponding ` +
181-
`React context consumer to ${displayName} in connect options.`
182-
)
181+
if (
182+
process.env.NODE_ENV !== 'production' &&
183+
!didStoreComeFromProps &&
184+
!didStoreComeFromContext
185+
) {
186+
throw new Error(
187+
`Could not find "store" in the context of ` +
188+
`"${displayName}". Either wrap the root component in a <Provider>, ` +
189+
`or pass a custom React context provider to <Provider> and the corresponding ` +
190+
`React context consumer to ${displayName} in connect options.`
191+
)
192+
}
183193

184194
// Based on the previous check, one of these must be true
185195
const store = didStoreComeFromProps ? props.store : contextValue.store

src/hooks/useReduxContext.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useContext } from 'react'
2-
import invariant from 'invariant'
32
import { ReactReduxContext } from '../components/Context'
43

54
/**
@@ -21,10 +20,11 @@ import { ReactReduxContext } from '../components/Context'
2120
export function useReduxContext() {
2221
const contextValue = useContext(ReactReduxContext)
2322

24-
invariant(
25-
contextValue,
26-
'could not find react-redux context value; please ensure the component is wrapped in a <Provider>'
27-
)
23+
if (process.env.NODE_ENV !== 'production' && !contextValue) {
24+
throw new Error(
25+
'could not find react-redux context value; please ensure the component is wrapped in a <Provider>'
26+
)
27+
}
2828

2929
return contextValue
3030
}

src/hooks/useSelector.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useReducer, useRef, useMemo, useContext } from 'react'
2-
import invariant from 'invariant'
32
import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
43
import Subscription from '../utils/Subscription'
54
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
@@ -95,8 +94,9 @@ export function createSelectorHook(context = ReactReduxContext) {
9594
? useDefaultReduxContext
9695
: () => useContext(context)
9796
return function useSelector(selector, equalityFn = refEquality) {
98-
invariant(selector, `You must pass a selector to useSelectors`)
99-
97+
if (process.env.NODE_ENV !== 'production' && !selector) {
98+
throw new Error(`You must pass a selector to useSelectors`)
99+
}
100100
const { store, subscription: contextSub } = useReduxContext()
101101

102102
return useSelectorWithStoreAndSubscription(

0 commit comments

Comments
 (0)