Repository URL to install this package:
|
Version:
4.1.3-alpha.1 ▾
|
import { isFunction } from 'exotic'
import { ClassicComponentClass, ReactNode } from 'react'
function fromClassToRender<Props = any, Context = any>(
Target: ClassicComponentClass
) {
const { render } = Target.prototype
if (isFunction(render)) {
throw new Error('missing render function ' + Target.name)
}
// args in the method
const arity = render.length
// already is functional
if (arity > 0) {
return render
}
// take in arguments
// put on function
// apply the function as the thisArg to the class
function statelessWrapper(props: Props, context: Context): ReactNode {
const thisArg = { props, context }
return render.call(thisArg, props, context)
}
// if (process.env.NODE_ENV !== 'production') {
// statelessWrapper.displayName =
// }
return statelessWrapper
}
export { fromClassToRender, fromClassToRender as asStateless }
export default fromClassToRender