在React中如何打印对象内的数组?

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

How to print an array inside of an object in react?

问题

以下是翻译好的部分:

export const Tab = (params) => {
  const [data, setData] = useState([{}])
    
  useEffect(() => {
    fetch('/eu/hostnames').then(
      response => response.json()
    ).then(data => {
      setData(data)
    })
  }, []);
    
  return (
    <>
      {console.log(data)}
      <header className={TabCSS.card}> 
        <h1 className={TabCSS.title}> {params.region_name} </h1>
        {data.map((item, idx) => {
          return <p className={TabCSS.server} key={idx}>{item}</p>;
        })} 
      </header>
    </>
  );
}
  
export default Tab;

如需帮助让这个工作起来,请提供更多具体的问题或代码上的错误信息,我将尽力协助。

英文:

I used Jsonify and I have this code

@app.route(&quot;/eu/hostnames&quot;)
def nameeu():
    global regionEU
    return jsonify(values = regionEU.serverName)

That returns this

在React中如何打印对象内的数组?

I tried using it in react with this

export const Tab = (params) =&gt; {
  const [data, setData] = useState([{}])
    
  useEffect(() =&gt; {
    fetch(&#39;/eu/hostnames&#39;).then(
      response =&gt; response.json()
    ).then(data =&gt; {
      setData(data)
    })
  }, []);
    
  return (
    &lt;&gt;
      {console.log(data)}
      &lt;header className={TabCSS.card}&gt; 
        &lt;h1 className={TabCSS.title}&gt; {params.region_name} &lt;/h1&gt;
        {data.map((item, idx) =&gt; {
          return &lt;p className={TabCSS.server} key={idx}&gt;{item}&lt;/p&gt;;
        })} 
      &lt;/header&gt;
    &lt;/&gt;
  );
}
  
export default Tab;

It then outputs this

在React中如何打印对象内的数组?

How do I get this thing working? I think it has something to do with the tab.js code in react. I tried a lot of things but nothing works. Plz help <3

答案1

得分: 1

Here's the translated content:

这一行是问题所在:

return &lt;p className={TabCSS.server} key={idx}&gt;{item}&lt;/p&gt;;

在你的初始情况下,你尝试渲染一个对象,{}。你还没有初始化状态以匹配数据,数据是一个带有 .values 键的对象。这应该可以解决问题:

export const Tab = (params) =&gt; {
  const [data, setData] = useState({ values: [] })
    
  useEffect(() =&gt; {
    fetch(&#39;/eu/hostnames&#39;).then(
      response =&gt; response.json()
    ).then(data =&gt; {
      setData(data)
    })
  }, []);
    
  return (
    &lt;&gt;
      {console.log(data)}
      &lt;header className={TabCSS.card}&gt; 
        &lt;h1 className={TabCSS.title}&gt; {params.region_name} &lt;/h1&gt;
        {data.values.map((item, idx) =&gt; {
          return &lt;p className={TabCSS.server} key={idx}&gt;{item}&lt;/p&gt;;
        })} 
      &lt;/header&gt;
    &lt;/&gt;
  );
}

如果你需要进一步的帮助,请告诉我。

英文:

This line is the problem:

return &lt;p className={TabCSS.server} key={idx}&gt;{item}&lt;/p&gt;;

You are trying to render an object in your initial case, {}. You also haven't initialized the state to match the data, which is an object with a .values key. This should fix it:

export const Tab = (params) =&gt; {
  const [data, setData] = useState({ values: [] })
    
  useEffect(() =&gt; {
    fetch(&#39;/eu/hostnames&#39;).then(
      response =&gt; response.json()
    ).then(data =&gt; {
      setData(data)
    })
  }, []);
    
  return (
    &lt;&gt;
      {console.log(data)}
      &lt;header className={TabCSS.card}&gt; 
        &lt;h1 className={TabCSS.title}&gt; {params.region_name} &lt;/h1&gt;
        {data.values.map((item, idx) =&gt; {
          return &lt;p className={TabCSS.server} key={idx}&gt;{item}&lt;/p&gt;;
        })} 
      &lt;/header&gt;
    &lt;/&gt;
  );
}

答案2

得分: 0

尝试使用以下方式而不是:

setData(data.values);

在你的API响应中,"values"数组包含了你想要显示的字符串。然而,当你使用setData(data)来设置状态时,你将整个响应对象分配给了状态变量,而不仅仅是"values"数组。

英文:

Instead of:

setData(data);

try using

setData(data.values);

In the response from your API, the "values" array contains the strings you want to display. However, when you set the state using setData(data), you're assigning the entire response object to the state variable, not just the "values" array.

huangapple
  • 本文由 发表于 2023年6月5日 04:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402335.html
匿名

发表评论

匿名网友

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

确定