0

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...

5
  • If you need access to child state in a parent container, move the state up to the parent. Commented May 16, 2016 at 14:16
  • What do you mean by moving the state up to parent? Moving logic from Test up to Class1? 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? Commented May 16, 2016 at 14:22
  • Data only flows form parent -> child. But like any other data you can also pass down callback functions through which you can pass child data as argument, which is handled in the parent. Commented May 16, 2016 at 14:30
  • If you want to inform your child that something has changed, then you pass down new props and listen to things in componentWillReceiveProps. Commented May 16, 2016 at 14:32
  • @Nickon Why not use children to pass Test instead of a prop? Commented May 16, 2016 at 14:59

2 Answers 2

1

Use props instead of state:

let { test } = this.props;

<div>{ React.cloneElement(test, {value: "Hello World"}) }</div>

And in Test:

<div>{ this.props.value }</div>

PS: Context means something else in React.

Sign up to request clarification or add additional context in comments.

Comments

0

A complete example:

class Class1 extends React.Component {
    render() {
        return (
            <Class2>
                <Test />
            </Class2>
        );
    }
}

class Class2 extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                {React.cloneElement(this.props.children, {text: "WHERE IS MY CONTEXT?"})}
            </div>
        );
    }
}

class Test extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>{ this.props.text }</div>
        );
    }
}

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.