英文:
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("/eu/hostnames")
def nameeu():
global regionEU
return jsonify(values = regionEU.serverName)
That returns this
I tried using it in react with this
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;
It then outputs this
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 <p className={TabCSS.server} key={idx}>{item}</p>;
在你的初始情况下,你尝试渲染一个对象,{}
。你还没有初始化状态以匹配数据,数据是一个带有 .values
键的对象。这应该可以解决问题:
export const Tab = (params) => {
const [data, setData] = useState({ values: [] })
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.values.map((item, idx) => {
return <p className={TabCSS.server} key={idx}>{item}</p>;
})}
</header>
</>
);
}
如果你需要进一步的帮助,请告诉我。
英文:
This line is the problem:
return <p className={TabCSS.server} key={idx}>{item}</p>;
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) => {
const [data, setData] = useState({ values: [] })
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.values.map((item, idx) => {
return <p className={TabCSS.server} key={idx}>{item}</p>;
})}
</header>
</>
);
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论