栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

React.Children.map中的React.cloneElement导致元素键改变

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

React.Children.map中的React.cloneElement导致元素键改变

问题不

cloneElement
在于改变您的钥匙。如文档中所述,cloneElement保留原始密钥。它
React.Children.map
为其添加了前缀。如果您不希望更改键,请使用
forEach
代替
map

这是React代码的摘录:

function escape(key) {  var escapeRegex = /[=:]/g;  var escaperLookup = {    '=': '=0',    ':': '=2',  };  var escapedString = ('' + key).replace(escapeRegex, function(match) {    return escaperLookup[match];  });  return '$' + escapedString;}function getComponentKey(component, index) {  // Do some typechecking here since we call this blindly. We want to ensure  // that we don't block potential future ES APIs.  if (    typeof component === 'object' &&    component !== null &&    component.key != null  ) {    // Explicit key    return escape(component.key);  }  // Implicit key determined by the index in the set  return index.toString(36);}function mapSingleChildIntoContext(bookKeeping, child, childKey) {  var {result, keyPrefix, func, context} = bookKeeping;  var mappedChild = func.call(context, child, bookKeeping.count++);  if (Array.isArray(mappedChild)) {    mapIntoWithKeyPrefixInternal(      mappedChild,      result,      childKey,      emptyFunction.thatReturnsArgument,    );  } else if (mappedChild != null) {    if (ReactElement.isValidElement(mappedChild)) {      mappedChild = ReactElement.cloneAndReplaceKey(        mappedChild,        // Keep both the (mapped) and old keys if they differ, just as        // traverseAllChildren used to do for objects as children        keyPrefix +          (mappedChild.key && (!child || child.key !== mappedChild.key) ? escapeUserProvidedKey(mappedChild.key) + '/' : '') +          childKey,      );    }    result.push(mappedChild);  }}function mapChildren(children, func, context) {  if (children == null) {    return children;  }  var result = [];  mapIntoWithKeyPrefixInternal(children, result, null, func, context);  return result;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号