I have an array. in which i want to iterate over and do conditional checks and display the correct element. i'm using functional ponent.
Below is an example :
import React, { useState, useEffect } from "react";
function Job(props) {
const [resp_data, setdata] = useState("");
const [look, setlook] = useState("");
useEffect(() => {
// some api calls happen here and response data is stored in state resp_data
}, [props]);
return (
<>
<div className="product_list">
{resp_data.length > 0
? resp_data.map((item) => {
{item.looking_for === "work" ? (
<div className="card-rows work" key={item.id}>
work
</div>
) : (<div className="card-rows no_work" key={item.id}>
No work
</div>)
}
})
: <div>array length is 0</div>}
</div>
</>
);
}
export default Job;
The response data received from api call happening inside useEffect is stored in state variable resp_data.
if resp_data length is not zero, i need to iterate over the list and check if the field value "looking_for" is equal to "work"
if true display a ponent , else display another ponent.
if resp_data length is zero display "array length is zero"
this i want to implement, but i could not find many answers for functional ponents. i have tried many possible ways by switching, changing the braces etc..my hard luck.
Any help is greatly appreciated.
I have an array. in which i want to iterate over and do conditional checks and display the correct element. i'm using functional ponent.
Below is an example :
import React, { useState, useEffect } from "react";
function Job(props) {
const [resp_data, setdata] = useState("");
const [look, setlook] = useState("");
useEffect(() => {
// some api calls happen here and response data is stored in state resp_data
}, [props]);
return (
<>
<div className="product_list">
{resp_data.length > 0
? resp_data.map((item) => {
{item.looking_for === "work" ? (
<div className="card-rows work" key={item.id}>
work
</div>
) : (<div className="card-rows no_work" key={item.id}>
No work
</div>)
}
})
: <div>array length is 0</div>}
</div>
</>
);
}
export default Job;
The response data received from api call happening inside useEffect is stored in state variable resp_data.
if resp_data length is not zero, i need to iterate over the list and check if the field value "looking_for" is equal to "work"
if true display a ponent , else display another ponent.
if resp_data length is zero display "array length is zero"
this i want to implement, but i could not find many answers for functional ponents. i have tried many possible ways by switching, changing the braces etc..my hard luck.
Any help is greatly appreciated.
Share Improve this question asked Jun 17, 2021 at 19:35 LiquidDeathLiquidDeath 1,8281 gold badge9 silver badges20 bronze badges 1-
2
hey, you missed the
return
keyword when you return the jsx. ` return <div className="card-rows work" key={item.id}> work </div>` – Yajat Vishwakk Commented Jun 17, 2021 at 19:46
1 Answer
Reset to default 5You are not returning anything from the 'map' function.
resp_data.map((item) => (
item.looking_for === "work" ? (
<div className="card-rows work" key={item.id}>
work
</div>
) : (<div className="card-rows no_work" key={item.id}>
No work
</div>)
)
)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260083a4619172.html
评论列表(0条)