React: Simple way to detect if a component is inside a table

Viewed 34

I know that I could use Context or props, but I want to see if there is something more generic or simpler to detect if I have my component standalone (let's say directly in a form) or if I have it inside a table.

In React JS.

1 Answers

In order to check if your current element is a descendant of some other element you can walk up the DOM starting with the element in question with Node.parentElement() (docs) and check whether that element has the attributes you're looking for (e.g in your case nodeName TABLE (docs)).

In order to get a handle on your react element, you can use a ref (docs)

Related