gpt4 book ai didi

reactjs - 找不到哪个 child 缺少关键 Prop 的 react 错误

转载 作者:行者123 更新时间:2023-12-05 08:08:27 25 4
gpt4 key购买 nike

我得到了一个很常见的警告:数组或迭代器中的每个子元素都应该有一个唯一的“键”属性。这通常很容易解决,但在这种情况下,我根本不可能找出问题产生的位置。

我的堆栈跟踪:

 in hoc (created by Connect(hoc))
in Connect(hoc) (at withTranslation.js:7)
in hoc (at ConnectedProtectedRoutes.js:26)
in Route (at ConnectedProtectedRoutes.js:44)
in ProtectedRoutes (created by Connect(ProtectedRoutes))

withTranslation 组件

export function withTranslation(CMP) {
var hoc = class extends React.Component {
render() {
return <CMP {...this.props} translate={translate} />
}
};
return hoc;
}

连接保护路由

const ProtectedRoutes = ({ token, authority, location }) => {
var a = [
createRouteWithRequirements(<Login key="1"/>, "/", [], { token, authority }, true),
createRouteWithRequirements(<Login key="2"/>, "/login", [], { token, authority }),
createRouteWithRequirements(<Register key="3"/>, "/register", [], { token, authority })
]

return a
};

const createRouteWithRequirements = (component, url, requirements, injections, exact) => {
return (
<Route //this is -> in Route (at ConnectedProtectedRoutes.js:44)
exact={!!exact}
key={url}
path={url}
render={() => {
if (requirements.includes("token") && !injections.token) {
return <Redirect to="/login" />
}

return component;
}}
/>
);
};

堆栈还在继续,但我猜问题出在某个地方。有什么线索吗?

最佳答案

在您的 ProtectedRoutes 组件中,您正在定义一个数组并在其他地方使用它。因此,每个数组都需要一个键,这就是您收到警告的原因。因此,处理这个数组的键。

关于reactjs - 找不到哪个 child 缺少关键 Prop 的 react 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630833/

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