英文:
My function does not render the data I supply to it. What do I do?
问题
const renderItem = (item) => {
return (
<div>
{Object.entries(item).map(([key, value]) => {
console.log(value); // 控制台打印值
return (
// 你尝试过的几种输出方式
// <td key={key}>{value}</td> // 使用 <tr></tr> 替代 <div> 包装
// <td>{value}</td>
// <p>Test</p> // 包装在 <div> 元素内
);
})}
</div>
);
};
在你的代码中,renderItem
函数遍历对象的属性并尝试输出相应的 HTML 元素,但它返回的内容没有被正确地放入输出中。你应该在 map
函数的回调中使用 return
来返回 JSX 元素,确保它们被正确输出到页面上。你可以根据需要选择其中一种输出方式,并根据你的需求进行修改。如果仍然有问题,请检查你的 React 组件的结构和数据传递,确保一切正常。
英文:
I am trying to write a table that should be writing out properties of an object in React. For some unknown reason I cannot get it to produce any output.
const renderItem = (item) => {
return (
<div>
{Object.entries(item).map(([key, value]) =>
{console.log(value)}
(
*variations in next segment
)
</div>
*in this line I tried:
- <td key={key}>{value}</td> // with <tr></tr> instead of wrapping <div> elements as shown above
- <td>{value}</td>
- <p> Test </p> // wrapped in <div> elements
The console.log(value) returns all the values it should so I know I supply the data correctly and it is iterating through the array as it should but somehow I cannot get it to produce any output inside of ().
I tried playing with many different variations, thought the problem might be in css but that is also not the case. I tried outputing data also simply in <div> or <p>, I tried moving the function to a whole new file, but nothing.
EDIT. Here is more info:
My main App.js:
import './App.css';
import HierarchyTable from './components/HierarchyTable.js';
import exampleData from './data/example.json';
function App() {
return (
<div className="App">
<HierarchyTable datafed={exampleData} ></HierarchyTable>
</div>
);
}
export default App;
My HierarchyTable.js
import React, { useState } from 'react';
import '../App.css';
export default function HierarchyTable({datafed}) {
const [data, setData] = useState(datafed)
const renderTable = (item) => {
return(
Object.entries(item).map(([key, value]) => {
if (key !== "children"){
renderItem(value)
}
else {
renderTable(value)
}
}
)
)
};
function renderItem (item) {
return Object.entries(item).map(([key, value]) => {
return (<tr><td key={key}>{value}</td></tr>)
}
)}
return (
<div>
<table>
<thead>
<tr>
{Object.entries(data[0].data).map(([key, value]) => (
<th
// className = "HeaderItem"
>{key}</th>
)
)}
</tr>
</thead>
<tbody>
{data.map((item) => renderTable(item))}
</tbody>
<tfoot>
</tfoot>
</table>
</div>
);
}
I tried doing it in many different variatons. The renderItem function gets the data I need as I can console.log it but it does not output the html elements as it should. It breaks my spirit.
答案1
得分: 2
你需要在map函数中正确返回值:
const renderItem = (item) => {
return (
<div>
{Object.entries(item).map(([key, value]) => {
console.log(value)
return value
//或者返回 (<p>{value}</p>)
})}
</div>
)
}
你很可能混淆了类似data.map(value => (value))的简写map函数。
如果你使用大括号{},你需要使用"return"明确返回值。如果你只使用(value),它等同于{return value}。
英文:
You need to properly return something from the map function:
const renderItem = (item) => {
return (
<div>
{Object.entries(item).map(([key, value]) => {
console.log(value)
return value
//or return (<p>{value}</p>)
})}
</div>
)
You most likely confused the shorthand map function that looks like data.map(value => (value))
If you use brackets {}, you need to specifically return something with "return". If you use (value) only, it is short from {return value}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论