react-onclickoutside icon indicating copy to clipboard operation
react-onclickoutside copied to clipboard

Cannot read property 'isReactComponent' of undefined

Open PaulLewisDancerace opened this issue 5 years ago • 19 comments

react-onclickoutside.es.js:329

New (13/08/19) NPX CRA build with react-onclickoutside - fails as soon as the functional component is wrapped.

PaulLewisDancerace avatar Aug 13 '19 16:08 PaulLewisDancerace

Could I ask you to add a minimal bit of code that shows this happening? Especially given that the indicated line is an empty line between a componentWillUnmount and render definition... =/

Pomax avatar Aug 20 '19 15:08 Pomax

Our set up is based exactly on your hooks example. We have an older app that still works, but if you create a new app with CRA and add react-onclickoutside as a dependency, then set up a simple example it will fail every time. We tested with older versions of React, duplicating existing projects and more.

We used this example...

import React, { useState } from "react";
import onClickOutside from "react-onclickoutside";

const Menu = () => {
  const [isOpen, setIsOpen] = useState(false);
  const toggle = () => setIsOpen(!isOpen);
  Menu.handleClickOutside = () => setIsOpen(false);
  return (
      //...
  )
};

const clickOutsideConfig = {
  handleClickOutside: () => Menu.handleClickOutside
};

export default onClickOutside(Menu, clickOutsideConfig);

PaulLewisDancerace avatar Aug 21 '19 08:08 PaulLewisDancerace

Same issue for me with functional component, with these dependencies :

"react-onclickoutside": "6.9.0" "react": "16.8.4", "react-dom": "16.8.4"

Works only if the component is created with the "function" keyword

jRichardeau avatar Aug 21 '19 21:08 jRichardeau

We encountered this same error happening on this line of code in react-onclickoutside, because WrappedComponent.prototype is not defined in some cases:

image

I haven't had time yet to narrow down exactly when this is the case, but in our situation, it appeared to happen when the wrapped component was a React forwardRef component.

jkillian avatar Sep 17 '19 21:09 jkillian

To add more details, React forwardRef components are plain objects and don't have a prototype property:

  return {
    $$typeof: REACT_FORWARD_REF_TYPE,
    render,
  };

Whereas class objects and functions do, so they don't cause the above issue. Other components, like React.memo components share a similar pattern to forwardRef ones, so they likely also will cause a break when used with onclickoutside.

https://github.com/facebook/react/blob/d862f0ea566be38d0c6a67447cb3a4bcf256cd55/packages/react/src/forwardRef.js#L49L52

jkillian avatar Sep 17 '19 22:09 jkillian

I ran into the same problem, when using next.js and a function component in this case. To workaround the issue I added a "prototype" field..

Edit: I was also using server side rendering where the error happened.

MyFunctionComponent.prototype = {}

Edit: @pedro-lb made the example more clear.

tammo avatar Nov 14 '19 11:11 tammo

Also having this problem with funcional components. I'm following the example.

Dropdown.prototype = {}

doesn't solve it for me.

pedro-lb avatar Nov 14 '19 18:11 pedro-lb

Using Gatsby, I also ran into this issue on running gatsby build. @tammo's comment solved it for me! :)

farazbhinder avatar Jan 09 '20 18:01 farazbhinder

I ran into this issue and checked the source code for further details... In the source code, you guys are accessing WrappedComponent.proptype to check if its react component or not... But since I was creating function using arrow function which won't even have a prototype.... for eg.

const Component = () => <><...></> //Componet.prototype is not available

here though Component is valid react component but Component.proptype is not available and is throwing uncaught error...

dreamerchandra avatar Jan 09 '20 20:01 dreamerchandra

@dreamerchandra I am also using functional components. Try adding the line after your functional component ends, like so const Component = () => <>...</>. Next line Component.proptype = {}

farazbhinder avatar Jan 09 '20 20:01 farazbhinder

I went back to using class components when using this package. This package really isn't ready for functional components anyways, as you cannot have multiple functional components render on the same page.

strongui avatar Jan 28 '20 15:01 strongui

I migrated to this solution for functional components: https://usehooks.com/useOnClickOutside/

liro avatar Feb 13 '20 11:02 liro

@jRichardeau thanks your suggestion worked for me! CRA using hooks

Works only if the component is created with the "function" keyword

burt23 avatar Jun 24 '20 13:06 burt23

In my case, using function instead of arrow function solved this issue

Thiry1 avatar Aug 18 '20 10:08 Thiry1

@jRichardeau thanks your suggestion worked for me! CRA using hooks

Works only if the component is created with the "function" keyword

can someone explain why here -> https://codesandbox.io/s/vn66kq7mml?file=/src/Menu.js it works. Actually, this is the sample that they have on the README file.

danielanthonyl avatar Sep 05 '20 00:09 danielanthonyl

I removed this package, it's super strange when the example works and your code does not, relying on the same logic. Migrated to https://github.com/Andarist/use-onclickoutside

dmishh avatar Nov 11 '20 11:11 dmishh

In my case, using function instead of arrow function solved this issue

This worked for me. However, it's strange how many people are all having this issue and it doesn't seem like any dev support has been given, since the docs say something like "only the latest versions of react will get updates and bug fixes."

BradenLinick avatar Nov 27 '20 16:11 BradenLinick

the best way to handle without this lib is this https://ahooks.js.org/hooks/dom/use-click-away/ it has the most comprehensive handling.

beqramo avatar Jan 04 '21 20:01 beqramo

I ran into the same problem, when using next.js and a function component in this case. To workaround the issue I added a "prototype" field..

Edit: I was also using server side rendering where the error happened.

MyFunctionComponent.prototype = {}

Edit: @pedro-lb made the example more clear.

Working for me in gatsby build

pacocom avatar Apr 16 '21 10:04 pacocom