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

Add ref for ComposedComponent #26

Closed

Conversation

jasonsanjose
Copy link
Contributor

Expose ref for the ComposedComponent to enable workflows that require ReactDOM.findDOMNode() on the WithStyles wrapper.

@koistya
Copy link
Member

koistya commented Mar 15, 2016

@jasonsanjose thanks for the PR! How would a test case for it look like?

@jasonsanjose
Copy link
Contributor Author

Trying to get jsdom to work in the unit tests so that ReactTestUtils.renderIntoDocument() works. Not working yet, but my proposal is:

  it('Should expose the ComposedComponent ref', () => {
    const WithStylesComponent = withStyles('')(
      class FooWithRef extends Component {
        render() {
          return <div ref="internalRef" />;
        }
      }
    ));
    const comp = ReactTestUtils.renderIntoDocument(<WithStylesComponent/>);
    expect(comp.refs.composedComponent.refs.internalRef).to.not.be.null;
  });

@frenzzy
Copy link
Member

frenzzy commented Mar 16, 2016

@jasonsanjose this feature already available, look at this: https://github.com/kriasoft/isomorphic-style-loader/blob/master/test/withStylesSpec.js#L40-L49

class YourComponent extends Component {
  render() {
    return <div ref="yourRef" />;
  }
}

const YourComponentWithStyles = withStyles('')(YourComponent);

YourComponentWithStyles.ComposedComponent.refs.yourRef // => DOM element

But probably the better way is to specify refs via props, it covers the case when your component is wrapped into multiple decorators:

class ChildComponent extends Component {
  render() {
    return (
      <div>
        <input ref={this.props.inputRef} />
      </div>
    );
  }
}

const DecoratedChildComponent = withStyles('')(ChildComponent);

class ParentComponent extends Component {
  componentDidMount() {
    this.input.focus();
  }

  render() {
    return (
      <DecoratedChildComponent inputRef={input => this.input = input} />
    );
  }
}

@jasonsanjose
Copy link
Contributor Author

@frenzzy thanks for the heads up. I missed this change #21. I'll take a look and reevaluate.

@frenzzy
Copy link
Member

frenzzy commented Oct 14, 2016

String refs are considered legacy, and will likely be deprecated at some point in the future.
Callback refs are preferred. More info in react docs: Refs to Components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants