Children can see the context they are composed in through:
this._reactInternalInstance._currentElement._owner._instance.__proto__.constructor.name
For example:
import React, { Component } from 'react';
class Warning extends Component {
render() {
return (
<div>{
"WARNING: " + this._reactInternalInstance._currentElement._owner._instance.__proto__.constructor.name + " has an error."
}</div>
);
}
}
export default Warning;
Do not believe this is parent-child communication. It is like standing in a room and seeing the containing walls from where you are. This is not the container (parent) communicating to you (child) but rather the child being context-aware.
Use this if you must, however, do note that it is part of React's internal instance and subject to change (do believe it is stable enough as of today).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…