Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

add a wrappedComponent ref on the Injector instance #90

Closed
wants to merge 1 commit into from
Closed

add a wrappedComponent ref on the Injector instance #90

wants to merge 1 commit into from

Conversation

rossipedia
Copy link
Contributor

As much as we'd like to keep things declarative in React, sometimes you have to get your hands dirty with imperative operations.

This PR allows the instance of the wrapped component to be accessed, rather than just the component class:

@inject('store')
class Child extends Component {
  ...

  doSomething() {
    ...
  }
}

/**
At this point, Child is a MobXStoreInjector wrapping the original Child,
and it has a property `wrappedComponent` which refers to the
original `Child` class itself.
*/

class Parent extends Component {
  constructor(props) {
    super(props);

    this.child = null;
  }

  onSomething = () => {
    // Currently this throws as child.wrappedComponent is undefined
    this.child.wrappedComponent.doSomething();
  }

  render() {
    const store = createStore();
    return (
      <Provider store={store}>
        <Child ref={instance => this.child = instance} />
      </Provider>
    );
  }
}

…s well, to be able to access it from a ref
@rossipedia
Copy link
Contributor Author

would wrappedInstance be a more appropriate property name?

@mweststrate
Copy link
Member

Merged this manually as #90. The instance will be exposed as wrappedInstance (wrappedComponent would break the current api and has it's own uses, like accessing propTypes). Will be part of next release. Thanks for the contribution!

mweststrate added a commit that referenced this pull request Aug 4, 2016
@mweststrate mweststrate closed this Aug 4, 2016
@rossipedia rossipedia deleted the wrapped-instance branch August 5, 2016 02:30
@rossipedia rossipedia restored the wrapped-instance branch August 5, 2016 02:30
@rossipedia rossipedia deleted the wrapped-instance branch April 13, 2017 16:24
@github-actions github-actions bot mentioned this pull request Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants