From f51e13fa4f76fef2c3de8273c01f90144b12ed86 Mon Sep 17 00:00:00 2001 From: Pablo Garcia Date: Wed, 11 Jul 2018 16:47:14 -0700 Subject: [PATCH] use react-is to validate the component passed to the function returned by connect() --- package.json | 1 + src/components/connectAdvanced.js | 3 ++- test/components/connect.spec.js | 12 +++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b872cb9fb..d2637f3e6 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "invariant": "^2.2.4", "loose-envify": "^1.1.0", "prop-types": "^15.6.1", + "react-is": "^16.4.1", "react-lifecycles-compat": "^3.0.0" }, "devDependencies": { diff --git a/src/components/connectAdvanced.js b/src/components/connectAdvanced.js index 46caa481d..80c7a9d89 100644 --- a/src/components/connectAdvanced.js +++ b/src/components/connectAdvanced.js @@ -1,6 +1,7 @@ import hoistStatics from 'hoist-non-react-statics' import invariant from 'invariant' import { Component, createElement } from 'react' +import * as ReactIs from 'react-is' import Subscription from '../utils/Subscription' import { storeShape, subscriptionShape } from '../utils/PropTypes' @@ -88,7 +89,7 @@ export default function connectAdvanced( return function wrapWithConnect(WrappedComponent) { invariant( - typeof WrappedComponent == 'function', + ReactIs.isValidElementType(WrappedComponent), `You must pass a component to the function returned by ` + `${methodName}. Instead received ${JSON.stringify(WrappedComponent)}` ) diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index da0f08071..1136079e4 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -1206,6 +1206,16 @@ describe('React', () => { ) }) + it('should not throw an error if a object created with React.forwardRef is passed to the function returned by connect', () => { + const ForwardRefComponent = React.forwardRef((props, ref) => +
+ ); + + expect(() => { + connect()(ForwardRefComponent) + }).not.toThrow() + }); + it('should throw an error if mapState, mapDispatch, or mergeProps returns anything but a plain object', () => { const store = createStore(() => ({})) @@ -1544,7 +1554,7 @@ describe('React', () => { it('should throw an error if the store is not in the props or context', () => { const spy = jest.spyOn(console, 'error').mockImplementation(() => {}) - + class Container extends Component { render() { return