UNPKG

1.45 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2import Children from '../children'; // 主要是把function节点,全部转换成string标签节点
3
4function renderJSXElement(element, context, updater) {
5 if (!element) return element;
6
7 var _element = element,
8 type = _element.type,
9 key = _element.key,
10 ref = _element.ref,
11 props = _element.props,
12 _element$_cache = _element._cache,
13 _cache = _element$_cache === void 0 ? {} : _element$_cache; // render children first
14
15
16 var children = Children.map(props.children, function (child) {
17 return renderJSXElement(child, context, updater);
18 });
19 element = {
20 type: type,
21 key: key,
22 ref: ref,
23 _cache: _cache,
24 props: _objectSpread(_objectSpread({}, props), {}, {
25 children: children
26 })
27 };
28
29 if (typeof type === 'function') {
30 // @ts-ignore
31 var newElement = type(element.props, context, updater);
32 if (!newElement) return newElement; // recursive render until type is string
33
34 return renderJSXElement(_objectSpread(_objectSpread({}, newElement), {}, {
35 // 保留原始的key和ref
36 key: key !== undefined ? key : newElement.key,
37 ref: ref !== undefined ? ref : newElement.ref
38 }), context, updater);
39 } // return element if type is string
40
41
42 return element;
43}
44
45export default (function (element, context, updater) {
46 return renderJSXElement(element, context, updater);
47});
\No newline at end of file