I have a react page that is rendering a list of products that are being returned from a GraphQL API. Once the products are returned, I'm storing them in a state and then render them. How do I only render products that meet certain conditions?
Right now, I'm mapping over the products and rendering each one. This is working correctly. But if I try to use conditional statements, I get errors in my IDE. This is the code I currently have. This is currently working:
async function contactAPI() {
return await axios({
url: 'https://graphqlexample.com/api/products',
method: 'post',
data: {
query: `
QUERY GOES HERE
`
}
})
}
function App() {
const [products, setProducts] = useState([]);
useEffect(() => {
async function getData() {
const res = await contactAPI();
setProducts(res.data.data.products);
}
getData();
}, []);
return (
<div>
<div>
{products.map(p =>
(
<div>
<ProductCard productName={p.productName} />
</div>
))}
</div>
</div>
);
}
I need to check p.productName to see if it meet certain conditions, and if it does, render it. If not, don't render. I tried using different conditionals inside of map, but keep getting errors.