Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React, get bound parent dom element name within component

Within my component, how can I access the name of the parent component it is nested inside?

So if my render is thus:

ReactDOM.render(       <RadialsDisplay data={imagedata}/>,       document.getElementById('radials-1') ); 

How can I retrieve the id name #radials-1 from within the component itself?

like image 258
Union find Avatar asked Sep 29 '15 22:09

Union find


People also ask

How do you get parent elements in React?

To get the parent height and width in React: Set the ref prop on the element. In the useEffect hook, update the state variables for the height and width. Use the offsetHeight and offsetWidth properties to get the height and width of the element.

What is React createRef ()?

This method is used to access any DOM element in a component and it returns a mutable ref object which will be persisted as long as the component is placed in the DOM. If we pass a ref object to any DOM element, then the. current property to the corresponding DOM node elements will be added whenever the node changes.

How do you select DOM elements in React?

At one point or another, every JavaScript developer has used the getElementById() method. It takes one argument id and returns an HTML element with that ID. This method is extremely useful for doing manual DOM mutations.


1 Answers

It probably makes the most sense to pass it as a property, but if you really need to get it programmatically, and from inside the component, you can wait for the component to mount, find its DOM node, and then look at its parent.

Here's an example:

class Application extends React.Component {   constructor() {     super();     this.state = { containerId: "" };   }    componentDidMount() {     this.setState({       containerId: ReactDOM.findDOMNode(this).parentNode.getAttribute("id")     });   }    render() {     return <div>My container's ID is: {this.state.containerId}</div>;   } }  ReactDOM.render(<Application />, document.getElementById("react-app-container")); 

Working demo: https://jsbin.com/yayepa/1/edit?html,js,output


If you do this a lot, or want to be really fancy, you could utilize a higher-order component:

class ContainerIdDetector extends React.Component {   constructor() {     super();     this.state = { containerId: "" };   }    componentDidMount() {     this.setState({       containerId: ReactDOM.findDOMNode(this).parentNode.getAttribute("id")     });   }    render() {     if (!this.state.containerId) {       return <span />;     } else {       return React.cloneElement(         React.Children.only(this.props.children),         { [this.props.property]: this.state.containerId }       );     }   } }  ContainerIdDetector.propTypes = {   property: React.PropTypes.string.isRequired }  // Takes an optional property name `property` and returns a function. This // returned function takes a component class and returns a new one // that, when rendered, automatically receives the ID of its parent // DOM node on the property identified by `property`. function withContainerId(property = "containerId") {   return (Component) => (props) =>     <ContainerIdDetector property={property}>       <Component {...props} />     </ContainerIdDetector> } 

Here, withContainerId is a function that takes an argument called property and returns a new function. This function can take a component type as its only argument, and returns a higher-order component. When rendered, the new component will render the passed component, with all its original props, plus an additional prop specifying the parent container's ID on the property specified by the property argument.

You can use them with ES7 decorators (as currently implemented) if you wish, or via a regular function call:

@withContainerId() class Application extends React.Component {   render() {     return <div>My containers ID is: {this.props.containerId}</div>;   } }  // or, if you don't use decorators: // // Application = withContainerId()(Application);  ReactDOM.render(<Application />, document.getElementById("react-app-container")); 

Working demo: https://jsbin.com/zozumi/edit?html,js,output

like image 136
Michelle Tilley Avatar answered Nov 09 '22 15:11

Michelle Tilley