1.0.1 • Published 7 years ago

react-hoc-with-ref v1.0.1

Weekly downloads
15
License
MIT
Repository
github
Last release
7 years ago

Common way to get access to wrapped component through HoC

npm install react-hoc-with-ref

App.js

import React, { Component, createElement } from 'react';
import createWithRef from 'react-hoc-with-ref';

const decorate = WrappedComponent =>
  class DecoratedApp extends createWithRef(WrappedComponent) {
    render() {
      return createElement(WrappedComponent, {
        ...this.props,
        ...this.getRefProps()
      });
    }
  };

@decorate
export default class App extends Component {
  render() {
    return (<div>Test</div>);
  }
}

index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import App from './App';

class TestComponent extends Component {
  appRef = (appComponent) => {
    if (appComponent) {
      console.log(appComponent.getWrappedInstance()); // App
    }
  };

  render() {
    return (<App ref={this.appRef} />);
  }
}

ReactDOM.render(<TestComponent />, document.querySelector('#app'));
1.0.1

7 years ago

1.0.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago