本文介绍了无法读取未定义 REACT 的属性“地图"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
构造函数(){极好的();这个.state = {清单:[],}}组件DidMount(){fetch('http://lospraianos-env-1.qu3skpxsmw.sa-east-1.elasticbeanstalk.com/api/materials').then(response => response.json()).then(resData => {this.setState({data: resData.results});})}<table className="纯表"><头><tr><th>id</th><th>产品</th><th>Quantidade</th></tr></头><身体>{this.props.lista.map(函数(数据){返回 (<tr 键={data.codMat}><td>{data.codMat}</td><td>{data.material}</td><td>{data.qntMin}</td></tr>);})}</tbody></表></div>我正在尝试从 API 获取信息并使用它制作一个表格,但我遇到了一些错误.
<块引用>无法读取未定义的属性地图"
我该如何处理?
解决方案 您正在设置对代码中不存在的状态变量数据的响应.您需要将其设置为 lista 而不是 fetch Api 调用中的数据,例如
this.setState({lista : resData.results});
在这里做条件检查和做.map
.map 没有返回
<tbody>{this.props.lista &&this.props.lista.map(数据 => (<tr 键={data.codMat}><td>{data.codMat}</td><td>{data.material}</td><td>{data.qntMin}</td></tr>))}</tbody>
.map 带返回
<tbody>{this.props.lista &&this.props.lista.map(data => {return (constructor(){
super();
this.state = {
lista: [],
}
}
componentDidMount(){
fetch('http://lospraianos-env-1.qu3skpxsmw.sa-east-1.elasticbeanstalk.com/api/materials')
.then(response => response.json())
.then(resData => {
this.setState( {data: resData.results});
})
}
<div>
<table className="pure-table">
<thead>
<tr>
<th>id</th>
<th>Produto</th>
<th>Quantidade</th>
</tr>
</thead>
<tbody>{
this.props.lista.map(function(data){
return (
<tr key={data.codMat}>
<td>{data.codMat}</td>
<td>{data.material}</td>
<td>{data.qntMin}</td>
</tr>
);
})
}
</tbody>
</table>
</div>
I'm trying to get info from the API and make a table with it but I'm getting some errors.
Cannot read property 'map' of undefined
how can I deal with it?
解决方案 You are setting response to a state variable data which doesn’t exist in your code. You need to set it to lista instead of data in fetch Api call like
this.setState({lista : resData.results});
And here do conditional check and do .map
.map without return
<tbody>{
this.props.lista && this.props.lista.map(data => (
<tr key={data.codMat}>
<td>{data.codMat}</td>
<td>{data.material}</td>
<td>{data.qntMin}</td>
</tr>
))
}
</tbody>
.map with return
<tbody>{
this.props.lista && this.props.lista.map(data => {
return (<tr key={data.codMat}>
<td>{data.codMat}</td>
<td>{data.material}</td>
<td>{data.qntMin}</td>
</tr>)
})
}
</tbody>
这篇关于无法读取未定义 REACT 的属性“地图"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
相关文档推荐
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
-
• 错误 [ERR_REQUIRE_ESM]:不支持 ES ...
-
• vue中yarn install报错:info There ...
-
• 为什么 Chrome(在 Electron 内部)会...
-
• “aria-hidden 元素不包含可聚焦元素...
-
• 使用选择器在 CSS 中选择元素的前一...
-
• js报错:Uncaught SyntaxError: Unex...
-
• layui怎么刷新当前页面?...
-
• 将模式设置为“no-cors"时使用 ...
-
• 类型错误:window.require 不是函数...
-
• 跨域读阻塞 (CORB)...
-
• node.js安装依赖包 yarn install inf...
-
• Google.com 和 clients1.google.com/...
-
• 错误 [ERR_REQUIRE_ESM]:不支持 ES ...
-
• vue中yarn install报错:info There ...
-
• 为什么 Chrome(在 Electron 内部)会...
-
• “aria-hidden 元素不包含可聚焦元素...
-
• 使用选择器在 CSS 中选择元素的前一...
-
• js报错:Uncaught SyntaxError: Unex...
-
• layui怎么刷新当前页面?...
-
• 将模式设置为“no-cors"时使用 ...
-
• 类型错误:window.require 不是函数...
-
• 跨域读阻塞 (CORB)...
-
• node.js安装依赖包 yarn install inf...
-
• Google.com 和 clients1.google.com/...