React
useImperativeHandle
for class component.
npm install @jswork/class-imperative-handle
import classImperativeHandle from '@jswork/class-imperative-handle';
class MyComponent extends Component {
handleRef = (inRoot) => {
const { forwardedRef } = this.props;
classImperativeHandle(forwardedRef, inRoot);
this.root = inRoot;
};
render() {
return (
<div
ref={this.handleRef}
{...this.props}
/>
);
}
}
export default React.forwardRef((props: ReactCheckboxProps, ref: any) => (
<MyComponent {...props} ref={ref} />
));
Code released under the MIT license.