Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
styleh-components / src / deps / fromClassToRender.ts
Size: Mime:
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