[英]Console.log() has a value but when I used it with map function it became undefined [ReactJS]
I'm very new to react.我很新来反应。
can you help me, please 😭你能帮帮我吗😭
I have spent a lot of time to research this error.我花了很多时间研究这个错误。 but I found nothing to solve my problem.
但我发现没有什么可以解决我的问题。
I use console.log()
it returns an array.我使用
console.log()
它返回一个数组。
but the next line, I used it with .map()
function on line 30
但是下一行,我
on line 30
它与.map()
函数一起on line 30
it throws an error.它抛出一个错误。
TypeError: Cannot read property 'map' of undefined
this is my render() function这是我的render()函数
render() {
return (
<div>
{console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)}
{this.state.allowedRoutes.map(route => (
<Route
exact
path={route.url}
component={allRoutes[route.component]}
key={route.url}
/>
))}
{this.state.redirectRoutes.map(url => (
<Redirect to={url} />
))}
</div>
);
}
it logged the {console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)}
's value.它记录了
{console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)}
的值。
but It cannot use map()
function.但它不能使用
map()
函数。
this is what my browser logged.这是我的浏览器记录的内容。
errrrrrrrrrrrrr (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
this is an error my browser:这是我的浏览器的错误:
as error implies, this.state.allowedRoutes
is undefined, either after logging the value or on subsequent renders.正如错误所暗示的那样,
this.state.allowedRoutes
是未定义的,无论是在记录值之后还是在后续渲染中。 you should also post your code too.你也应该发布你的代码。
But for a workarround you can check not to be undefined
with this.state.allowedRouter && this.state.allowedRoutes.map()
但是对于解决方法,您可以使用
this.state.allowedRouter && this.state.allowedRoutes.map()
检查是否undefined
As a side note, apparently you are updating your state in render method which is also not correct.作为旁注,显然您正在更新渲染方法中的状态,这也不正确。
It's likely that at some point you're updating your state and by mistake deleting allowedRoutes
.很可能在某个时候您正在更新您的状态并错误地删除了
allowedRoutes
。 If you set your state without destructuring it first, all previous data will be removed, examples:如果您在不先解构的情况下设置状态,则将删除所有先前的数据,例如:
// WRONG - STATE BECOMES { allowedRoutes: myArray }
this.setState({
allowedRoutes: myArray
});
// RIGHT - STATE BECOMES { all previous properties plus allowedRoutes: myArray }
this.setState({
...this.state,
allowedRoutes: myArray
});
Also be sure to be initializing allowedRoutes as an empty array if you want to use map
without checking the value first ( map
is an array method).如果您想使用
map
而不先检查值( map
是一个数组方法),请确保将 allowedRoutes 初始化为空数组。
If you're using classes:如果您正在使用类:
constructor(props) {
super(props);
this.state = {allowedRoutes: []};
}
If you're using hooks:如果您使用钩子:
const [allowedRoutes, setAllowedRoutes] = useState([]);
Your picture shows that the exception was thrown inside .map
:您的图片显示异常是在
.map
中引发的:
this.state.allowedRoutes.map(
// the exception was thrown here
...
)
Please notice at this line, you can see allRoutes
is not defined in the current scope.请注意这一行,您可以看到
allRoutes
未在当前范围内定义。
component={allRoutes[route.component]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.