An open API service indexing awesome lists of open source software.

https://github.com/fakundo/react-hoc-with-ref

Common way to get access to wrapped component through HoC
https://github.com/fakundo/react-hoc-with-ref

Last synced: 6 months ago
JSON representation

Common way to get access to wrapped component through HoC

Awesome Lists containing this project

README

          

## Common way to get access to wrapped component through HoC

```
npm install react-hoc-with-ref
```

App.js
```javascript
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 (

Test
);
}
}
```

index.js
```javascript
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 ();
}
}

ReactDOM.render(, document.querySelector('#app'));
```