From 3cd7fa4c8a4ac080207ecb48ffaebb2693c83f3a Mon Sep 17 00:00:00 2001 From: "tao.xu" Date: Tue, 7 Apr 2020 12:25:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86children.map?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 25 +++++++++++++++++++++++-- src/react/ReactElement.js | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..65936e8 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,29 @@ -function mapChildren(children, func, context) { +import {ReactElement} from './ReactElement' +function mapChildren(children, callback, context) { //TODO实现此mapChildren方法 - return children; + var newChildren = []; + var indexObj = { index: 0 };//需要是个引用类型,值类型的话,递归函数内修改了,但是外边不知道,仍然以旧值循环 + traversalAll(children, newChildren, indexObj, callback); + return newChildren; } +function traversalAll(children, res, indexObj, callback, prefix = '') { + if (!children) { + return null; + } + if (Array.isArray(children)) { + for (let k = 0; k < children.length; k++) { + let spelator = prefix ? ':' : '.' + let newPrefixName = prefix + spelator + k; + traversalAll(children[k], res, indexObj, callback, newPrefixName); + } + } else { + // console.log(`${prefix}`); + var oldElement = children; + var clonedElement = ReactElement(oldElement.type, prefix, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props); + res.push(callback(clonedElement, indexObj.index++)) + } +} export { mapChildren as map, }; \ No newline at end of file diff --git a/src/react/ReactElement.js b/src/react/ReactElement.js index 25fdf5e..0d33b60 100644 --- a/src/react/ReactElement.js +++ b/src/react/ReactElement.js @@ -59,7 +59,7 @@ export function createElement(type, config, children) { type, key, ref, self, source, ReactCurrentOwner.current, props ) } -function ReactElement(type, key, ref, _self, _source, _owner, props) { +export function ReactElement(type, key, ref, _self, _source, _owner, props) { const element = { $$typeof: REACT_ELEMENT_TYPE, type, -- Gitee From 9837d96cda15587489da9e9d272fcfc6c07590b3 Mon Sep 17 00:00:00 2001 From: "tao.xu" Date: Tue, 7 Apr 2020 12:28:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index 65936e8..d9c2968 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -18,7 +18,7 @@ function traversalAll(children, res, indexObj, callback, prefix = '') { traversalAll(children[k], res, indexObj, callback, newPrefixName); } } else { - // console.log(`${prefix}`); + // console.log(`${prefix}`);key字符串 var oldElement = children; var clonedElement = ReactElement(oldElement.type, prefix, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props); res.push(callback(clonedElement, indexObj.index++)) -- Gitee From ce52985f841aaabe233784f4cf06ad1170cdebcf Mon Sep 17 00:00:00 2001 From: luckycuiying <331502715@qq.com> Date: Tue, 7 Apr 2020 12:32:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index d9c2968..3ad6146 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -18,7 +18,6 @@ function traversalAll(children, res, indexObj, callback, prefix = '') { traversalAll(children[k], res, indexObj, callback, newPrefixName); } } else { - // console.log(`${prefix}`);key字符串 var oldElement = children; var clonedElement = ReactElement(oldElement.type, prefix, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props); res.push(callback(clonedElement, indexObj.index++)) -- Gitee