Skip to content

Instantly share code, notes, and snippets.

@oliviertassinari
Last active January 6, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliviertassinari/fa1cd34a3fff67553631606109bed124 to your computer and use it in GitHub Desktop.
Save oliviertassinari/fa1cd34a3fff67553631606109bed124 to your computer and use it in GitHub Desktop.
Get a reference on the DOM element.
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
class RootRef extends React.Component {
componentDidMount() {
this.props.rootRef(ReactDOM.findDOMNode(this))
}
componentWillUnmount() {
this.props.rootRef(null)
}
render() {
return this.props.children
}
}
RootRef.propTypes = {
children: PropTypes.element.isRequired,
rootRef: PropTypes.func.isRequired,
};
export default RootRef;
@oliviertassinari
Copy link
Author

oliviertassinari commented Jan 6, 2018

Example of usage:

<RootRef
  rootRef={node => {
    console.log(node)
  }}
>
  <Paper />
</RootRef>

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