I have Class1 which is only a container for Class2. I declare Test component in Class1. Now I want to pass Test into Class2 as a parameter.
Is it possible to access Test component's context being inside Class2 in place where I put a comment in?
export default class Class1 extends React.Component {
render() {
let test = (<Test />);
return (
<Class2 test={test} />
);
}
}
export default class Class2 extends React.Component {
constructor(props) {
super(props);
}
render() {
let { test } = this.props;
// how to access Test class context?
test.setValue("WHERE IS MY CONTEXT?");
return (
<div>{ test }</div>
);
}
}
export default class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ""
};
}
setValue(val) {
this.setState({
value: val
});
}
render() {
return (
<div>{ this.state.value }</div>
);
}
}
I tried to find something on Web and to examine the test object, but I found nothing... When I try to access test.props directly I get a React error that props are read only and can't be accessed...
Testup toClass1? I don't want this. My project is simple, but generic Wizard component. It works in this way: I have a class that is actually a body component for the Wizard. I pass this class to the Wizard, but I can have more than one template defined inside. So I want to inform my Test template to set the page and refresh itself. It is written in the same way as this example in the question. I know how to pass context down to the child, but is it possible in the opposite way, from child to parent?componentWillReceiveProps.Testinstead of a prop?