gpt4 book ai didi

javascript - Warning : flattenChildren(. ..): Encountered two children with the same key/Child keys must be unique

转载 作者:可可西里 更新时间:2023-11-01 01:42:55 25 4
gpt4 key购买 nike

昨天我将 react-router-dom 添加到我的项目中,现在当我离开并返回导航中的 Sky 元素时,它会重新加载天空,我得到

Warning: flattenChildren(...): Encountered two children with the same key, element-id-50. Child keys must be unique; when two children share a key, only the first child will be used.

(上面使用的数字 50 只是一个例子,它每次都会抛出这个错误 ~40 次,所有的 id 都不同)

问题似乎出在我的 sky.js 文件中:

componentWillMount() {
this.props.dispatch(requestSkySetup());
this.props.dispatch(requestAllElements());

this.setState({loadedSky: true, loadedElements: true});
}

因为每次我转到另一个屏幕时,这个组件都会卸载,然后在我回来时重新安装。

receiveSkySetup 完成后,sky.js 中的render 函数创建了一堆名为Sector 的div s 和每个 Sector 创建一些称为 Slot 的 div。

然后在 Slot.render 里面我有:

return connectDropTarget(
<div className={showOutline ? 'slot showOutline' : 'slot'} style={style} onClick={interactable ? this.handleClick : null}>
{
elements
.map(e => (
<SkyElement
id={e.id}
key={`element-id-${e.id}`}
title={e.title}
size={150}
opacity={e.opacity}
glow={e.glow}
color={e.color}
sectorId={e.sectorId}
slotId={e.id}
dispatch={this.props.dispatch}
isDragging={false}
transformElement={false} />
))
}
</div>
);

上面 SkyElement 调用中的 key 元素是每次挂载时抛出 40 多个错误的原因。

如果需要,很乐意提供更多代码。

任何帮助都会非常有帮助。谢谢!

编辑:控制台日志元素

再深入一点,我店里的商品翻了一番。

因此,在 sky 选项卡的第二次渲染中,元素 ID 的完整列表是 ["0", "1", "2", "3", "4 ", "5", "6", "7", "17", "18", "19", "55", "56", "57", "58", "59", "60", “61”、“62”、“63”、“64”、“65”、“66”、“67”、“77”、“78”、“0”、“1”、“2”、“3” ", "4", "5", "6", "7", "17", "18", "19", "55", "56", "57", "58", "59", “60”、“61”、“62”、“63”、“64”、“65”、“66”、“67”、“77”、“78”]

在第 3 次渲染时,元素 0-78(从上面的数组中应用的 id)将再次添加到数组中

Slot.js

const mapStateToProps = ({elements}, ownProps) => {
return {
elements: getElementsBySlotId(elements, ownProps.id),
};
};

elements 这里将是 n Sky 已完成的加载次数的 n 倍。

sky.js

const mapStateToProps = ({sky, elements}) => {
return {
sectors: getSky(sky).sectors,
elements: getElementsByKeyName(elements, 'visibleElements'),
unplacedElements: getElementsByKeyName(elements, 'unplacedElements'),
};
};

打印 elements.length 我看到它们在这里也加倍了。 Slot.js 是从同一家商店提取的,所以这是有道理的

在我的 elements/reducer.js

case 'receiveAllElements':
const visibleElements = {};
const unplacedElements = {};

const elements = action.elements.reduce((result, index) => {
result[`${index.id}`] = index;
return result;
}, {});

const keys = Object.keys(elements);
for (const key of keys) {
const e = elements[key];

if (e.sectorId === null) {
unplacedElements[key] = e;
} else {
visibleElements[key] = e;
}
}

const visibleIds = Object.keys(visibleElements);
const unplacedIds = Object.keys(unplacedElements);
console.log(visibleIds);
console.log(unplacedIds); // logging these, the numbers are consistent and don't double, triple etc with each load

return {
...state,
elementsMap: {
...state.elementsMap,
...elements,
},
visibleElements: [...state.visibleElements, ...visibleIds],
unplacedElements: [...state.unplacedElements, ...unplacedIds],
};

也许里面有什么东西导致计数加倍?

最佳答案

这里的问题是

    return {
...state,
elementsMap: {
...state.elementsMap,
...elements,
},
visibleElements: [...state.visibleElements, ...visibleIds],
unplacedElements: [...state.unplacedElements, ...unplacedIds],
};

visibleElements(和 unplacedElements 值)。

[...state.visibleElements, ...visibleIds] 将连接 2 个数组,因此每次我返回 Sky 选项卡时都会触发此代码,它将 ...visibleIds 中的新 ID 添加到我在 ...state.visibleElements 中已有的数组,并将值加倍

关于javascript - Warning : flattenChildren(. ..): Encountered two children with the same key/Child keys must be unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45314006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com