我的函数没有呈现我提供的数据。我该怎么办?

huangapple go评论66阅读模式
英文:

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) =&gt; {
  return (
&lt;div&gt;

  {Object.entries(item).map(([key, value]) =&gt; 
          {console.log(value)}
    (
 *variations in next segment    
 )
&lt;/div&gt;

*in this line I tried:

  1. <td key={key}>{value}</td> // with <tr></tr> instead of wrapping <div> elements as shown above
  2. <td>{value}</td>
  3. <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 &#39;./App.css&#39;;
import HierarchyTable from &#39;./components/HierarchyTable.js&#39;;
import exampleData from &#39;./data/example.json&#39;;

function App() {

  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;HierarchyTable datafed={exampleData} &gt;&lt;/HierarchyTable&gt;
    &lt;/div&gt;
  );
}

export default App;

My HierarchyTable.js

import React, { useState } from &#39;react&#39;;
import &#39;../App.css&#39;; 

export default function HierarchyTable({datafed}) {
  const [data, setData] = useState(datafed)

  const renderTable = (item) =&gt; {
    return(
    Object.entries(item).map(([key, value]) =&gt; {
     
        if (key !== &quot;children&quot;){
          renderItem(value)
        }
        else {
          renderTable(value)
        }
      }
    )
   )
  };

  function renderItem (item) {
  return Object.entries(item).map(([key, value]) =&gt; { 
    
      return (&lt;tr&gt;&lt;td key={key}&gt;{value}&lt;/td&gt;&lt;/tr&gt;)
      
    }
    )}
 
  return (
   &lt;div&gt;
   
    &lt;table&gt;
      &lt;thead&gt; 
        &lt;tr&gt;
             {Object.entries(data[0].data).map(([key, value]) =&gt; ( 
              &lt;th 
              // className = &quot;HeaderItem&quot;
              &gt;{key}&lt;/th&gt;
          )
          )}
        &lt;/tr&gt;
        &lt;/thead&gt;
        
       &lt;tbody&gt;
       {data.map((item) =&gt; renderTable(item))}
      &lt;/tbody&gt;
      &lt;tfoot&gt;
   
  &lt;/tfoot&gt;
   &lt;/table&gt;
      &lt;/div&gt;
  );
}

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) =&gt; {
  return (
&lt;div&gt;
  {Object.entries(item).map(([key, value]) =&gt; {
        console.log(value)
        return value
        //or return (&lt;p&gt;{value}&lt;/p&gt;)
    })}
&lt;/div&gt;
)

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}

huangapple
  • 本文由 发表于 2023年2月13日 23:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定