Skip to content
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

findDOMNode doesn't work with this as argument #13652

Closed
tldrRD opened this issue Sep 14, 2018 · 4 comments
Closed

findDOMNode doesn't work with this as argument #13652

tldrRD opened this issue Sep 14, 2018 · 4 comments

Comments

@tldrRD
Copy link

tldrRD commented Sep 14, 2018

I'm getting an Uncaught Error: Unable to find node on an unmounted component. error with this component. It is a strange behaviour as handleClickEvent is attached only when the component is already mounted.

The issue is with findDOMNode(this). For some reason it won't work. Is this the desired behaviour or I am missing something?

I'm running everything with Storybook and styled.components.
React/ReactDOM versions are 16.4.0.

import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';

export default class extends Component {
  static propTypes = {
    children: PropTypes.node,
    onClick: PropTypes.func,
  };

  componentDidMount() {
    document.addEventListener('click', this.handleClickEvent, { capture: true });
  };

  componentWillUnmount() {
    document.removeEventListener('click', this.handleClickEvent, { capture: true });
  };

  handleClickEvent = (event) => {
    const el = findDOMNode(this);
    
    if (
      !this.props.onClick ||
      !el ||
      (event.target instanceof Node && el.contains(event.target))
    ) {
      return;
    }

    this.props.onClick(e);
  };

  render() {
    return this.props.children;
  };
}
@gaearon
Copy link
Collaborator

gaearon commented Sep 14, 2018

Can you create an isolated fiddle demonstrating this?

@Simek
Copy link
Contributor

Simek commented Sep 15, 2018

I was not able to reproduce this issue. All three logs that I defined appears correctly in console in order this (React class), then HTML node found by findDOMNode and at the end click event passed to children.

Test case that I created do not produce any warnings or errors using 16.4.0 or 16.5.1 versions: https://codesandbox.io/s/7klp1j38rx

Feel free to edit/fork my fiddle and correct it to deliver behavior as mentioned in issue.

@tldrRD
Copy link
Author

tldrRD commented Sep 17, 2018

Sorry for taking so long to answer.

I have tried @Simek's code on codesandbox with some modifications to resemble how I use the component and it worked as intended.

I did not manage to install storybook on codesandbox as I don't use it often. I will try to install storybook and keep you updated if I manage to replicate the issue.

At this point I assume it is a problem with storybook and findDOMNode(this). Maybe something with multiple react instances?

Will keep investigating locally.

@tldrRD
Copy link
Author

tldrRD commented Sep 17, 2018

It was a stupid error on my side, the versions of react and react-dom were different. I am so sorry for making a fuss out of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants