Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child components can steal their parent's ref. #158

Closed
mthadley opened this issue Sep 20, 2016 · 1 comment
Closed

Child components can steal their parent's ref. #158

mthadley opened this issue Sep 20, 2016 · 1 comment
Assignees
Labels

Comments

@mthadley
Copy link

This one is more convoluted than I like to normally report, but I still managed to run into it.

Given the following structure:

import Component from 'metal-jsx';

class Form extends Component {
    render() {
        return <form>{this.props.children}</form>;
    }
}

class Wrapper extends Component {
    render() {
        return <div class="well">{this.props.children}</div>;
    }
}

class Card extends Component {
    rendered() {
        console.log("Card:", this.refs.form);
    }

    render() {
        // Here is where things break down, we need to both assign the
        // JSX expression to a variable, and then wrap it in another component.
        //
        // If we just inline the value of content, or change Wrapper to something
        // like a div instead, everything works fine.
        const content = <div>{this.props.children}</div>;
        return <Wrapper>{content}</Wrapper>
    }
}

class App extends Component {
    rendered() {
        console.log("App:", this.refs.form);
    }

    render() {
        return (
            <Card>
                <Form ref="form" />
            </Card>
        );
    }
}

export default App;

You will see the following in the console:

Card: Form {disposed_: false, events_: Array[0], maxListeners_: 10, shouldUseFacade_: false, attachedListeners_: null…}
App: undefined
@mairatma
Copy link
Contributor

Hmmm interesting, I wonder why assigning to a variable causes this. Thanks for reporting, I'll investigate.

@mairatma mairatma added the bug label Sep 21, 2016
@mairatma mairatma self-assigned this Sep 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants