Skip to content

Commit

Permalink
fix(runtime): 使用函数式组件作为入口组件不会报错
Browse files Browse the repository at this point in the history
close #6517
  • Loading branch information
yuche committed Jun 8, 2020
1 parent 423ff51 commit a6a02a6
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/taro-runtime/src/dsl/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import { document } from '../bom/document'
import { injectPageInstance } from './common'
import { isBrowser } from '../env'

function isClassComponent (R: typeof React, component): boolean {
return isFunction(component.render) ||
!!component.prototype?.isReactComponent ||
component.prototype instanceof R.Component // compat for some others react-like library
}

export function connectReactPage (
R: typeof React,
id: string
) {
const h = R.createElement
return (component: ReactPageComponent): React.ComponentClass<PageProps> => {
// eslint-disable-next-line dot-notation
const isReactComponent = isFunction(component['render']) ||
!!component.prototype?.isReactComponent ||
component.prototype instanceof R.Component // compat for some others react-like library
const isReactComponent = isClassComponent(R, component)

const inject = (node?: Instance) => node && injectPageInstance(node, id)
const refs = isReactComponent ? { ref: inject } : { forwardedRef: inject }
Expand Down Expand Up @@ -83,6 +87,7 @@ export function createReactApp (App: React.ComponentClass, react: typeof React,
ensure(!!ReactDOM, '构建 React/Nerv 项目请把 process.env.FRAMEWORK 设置为 \'react\'/\'nerv\' ')

const ref = R.createRef<ReactAppInstance>()
const isReactComponent = isClassComponent(R, App)

let wrapper: AppWrapper

Expand Down Expand Up @@ -115,9 +120,15 @@ export function createReactApp (App: React.ComponentClass, react: typeof React,
this.elements.push(page())
}

let props: React.Props<any> | null = null

if (isReactComponent) {
props = { ref }
}

return R.createElement(
App,
{ ref },
props,
isBrowser ? R.createElement('div', null, this.elements.slice()) : this.elements.slice()
)
}
Expand Down

0 comments on commit a6a02a6

Please sign in to comment.