[英]React cannot access multidimensional array state value
In my react application I am setting the state value as an array using the below code.在我的 React 应用程序中,我使用以下代码将 state 值设置为数组。
const [ds_data, setDsData] = useState([]);
let ds_arr = res.data.map((elem, index) => {
return elem.split(" ")
})
setDsData(ds_arr)
The values in the array when I use console.log(ds_arr) are当我使用 console.log(ds_arr) 时数组中的值是
The value I am getting from the server is given below.我从服务器获得的值如下所示。
The problem is when I try to display the value of the multidimensional array using {ds_data[0][0]}
, I am getting the error:问题是当我尝试使用
{ds_data[0][0]}
显示多维数组的值时,出现错误:
Uncaught TypeError: Cannot read properties of undefined (reading '0')
未捕获的类型错误:无法读取未定义的属性(读取“0”)
in the console.在控制台中。 When I used
{ds_data[0]}
it is showing all the array values as a single string:当我使用
{ds_data[0]}
时,它将所有数组值显示为单个字符串:
363861319e9d5720089762a51d24a86dd1402dbba64771c9
The data is displayed inside a bootstrap modal component数据显示在引导模式组件内
The problem was that the state was not set initially, so I have to check if the state was set using the code {Array.isArray(ds_data) && Array.isArray(ds_data[0])? ds_data[0][0]: ""}
问题是 state 最初没有设置,所以我必须检查 state 是否使用代码
{Array.isArray(ds_data) && Array.isArray(ds_data[0])? ds_data[0][0]: ""}
{Array.isArray(ds_data) && Array.isArray(ds_data[0])? ds_data[0][0]: ""}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.