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
2
Answers
Instead of:
try using
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.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: