-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Link] TypeError: Cannot read property 'addEventListener' of undefined #16454
Comments
@7s4r This is mostly a documentation issue. You should check https://material-ui.com/components/links/#third-party-routing-library regarding how react-router should be used with the Link component. However, I believe we can improve the DX around this issue. I would propose the following diff: diff --git a/packages/material-ui/src/Link/Link.js b/packages/material-ui/src/Link/Link.js
index e82c8b0e3..f559a9621 100644
--- a/packages/material-ui/src/Link/Link.js
+++ b/packages/material-ui/src/Link/Link.js
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import { capitalize } from '../utils/helpers';
import withStyles from '../styles/withStyles';
+import { elementTypeAcceptingRef } from '@material-ui/utils';
import { useIsFocusVisible } from '../utils/focusVisible';
import { useForkRef } from '../utils/reactHelpers';
import Typography from '../Typography';
@@ -142,7 +143,7 @@ Link.propTypes = {
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
- component: PropTypes.elementType,
+ component: elementTypeAcceptingRef,
/**
* @ignore
*/
diff --git a/packages/material-ui/src/utils/focusVisible.js b/packages/material-ui/src/utils/focusVisible.js
index f20ddb884..6cae7319a 100644
--- a/packages/material-ui/src/utils/focusVisible.js
+++ b/packages/material-ui/src/utils/focusVisible.js
@@ -127,6 +127,15 @@ export function useIsFocusVisible() {
const ref = React.useCallback(instance => {
const node = ReactDOM.findDOMNode(instance);
if (node != null) {
+ if (!node.ownerDocument) {
+ throw new Error(
+ [
+ 'Material-UI: the ref forwarding logic is not correct.',
+ `An Element is expected but found ${node}.`,
+ ].join('\n'),
+ );
+ }
+
prepare(node.ownerDocument);
}
}, []); cc @eps1lon. Do you want to submit a pull request? :) |
Though we don't need the manual prepare step. This can be delegated to |
Thank you @oliviertassinari ! It works adding this code:
|
@eps1lon The ButtonBase component fails identically to the Link component. I believe the components delegate the preparation step to when the ref triggers. |
Yep which makes more sense in a concurrent environment or non-ref environment. Not sure what you're trying to say. Breadcrumbs just needs to use the full |
@eps1lon What do you mean by full hook? The error reported in this issue originate from the Link component. Why is the breadcrumb a variable of the equation? |
I was under the impression that patch targeted Link which was still using prepare. There's no need to add an additional error in the |
@eps1lon I wish <Button component={(props) => props.children}> but doesn't warn for import { Link as RouterLink } from "react-router-dom";
<Button component={RouterLink}> No matter what, diff --git a/packages/material-ui/src/Link/Link.js b/packages/material-ui/src/Link/Link.js
index e82c8b0e3..f559a9621 100644
--- a/packages/material-ui/src/Link/Link.js
+++ b/packages/material-ui/src/Link/Link.js
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import { capitalize } from '../utils/helpers';
import withStyles from '../styles/withStyles';
+import { elementTypeAcceptingRef } from '@material-ui/utils';
import { useIsFocusVisible } from '../utils/focusVisible';
import { useForkRef } from '../utils/reactHelpers';
import Typography from '../Typography';
@@ -142,7 +143,7 @@ Link.propTypes = {
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
- component: PropTypes.elementType,
+ component: elementTypeAcceptingRef,
/**
* @ignore
*/ Would already be a great step forward, @7s4r si tu veux contribuer au projet :) ? |
Huh that's a weird one. I guess it doesn't warn because it's a class component at which point I don't understand why we can't get the correct host instance (that's what the findDOMNode fallback is for). I can't get the codesandbox to run (had to add 2 missing dependencies and then it failed in Your patch is a great step but I'm interested in a reproducible example for that error message. |
@eps1lon For reproducing, I had to use the export as zip feature of Codesandbox: It seems that findDOMNode returns a weird object with |
Yeah so it's an issue with It's a slight variation of the previous issue where we prepared in the effect phase. In Not sure if proper ref forwarding would fix it but adding an error there wouldn't solve the issue (because the usage is perfectly fine). We would need to silence it of which I am no fan. I would need to look into why |
@eps1lon I think that you are right. I would suspect a bug with findDOMNode and react-test-renderer .toJSON(). |
|
I can take a shot at this if its ok with you |
@icancode007 Sorry, we already have @Nikhil-Pavan-Sai on it. |
When I'm running unit tests with Jest, I get this error:
TypeError: Cannot read property 'addEventListener' of undefined
I think this is related to this issue and this pull request. As breadcrumb component was added recently from lab to core, it is missing fallback to ownerWindow.
Expected Behavior 🤔
Unit test should pass without errors
Current Behavior 😯
Unit test fail with this stack trace:
Steps to Reproduce 🕹
Link: https://codesandbox.io/s/create-react-app-9e5kw
Context 🔦
I was trying to unit test with Jest my component.
Your Environment 🌎
The text was updated successfully, but these errors were encountered: