Mitch VanDuyn
Mitch VanDuyn

Reputation: 2878

How do I get the owner or parent of a react component

Is there any way for a react child component to find out its parent without explicitly adding a ref on the parent, that is passed to the child?

Perhaps some undocumented property ?

Upvotes: 1

Views: 1081

Answers (1)

Silfverstrom
Silfverstrom

Reputation: 29348

There is actually a _owner property, but it's intended to be private, so it is not guaranteed that it will remain accessible in future react releases.

componentDidMount() {
    console.log(this._owner.props); // Logs parent props-object.
}

However, as this will probably stop working one day and I want my conscience to be clean, it forces me to suggest that you pass a callback or refactor how the data flows in your application.

Upvotes: 2

Related Questions