I have a simple list created using react and fetching data using axios as the following
https://codesandbox.io/s/old-worker-6k4s6
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import axios from "axios";
export default () => {
const [list, setList] = React.useState([]);
useEffect(() => {
axios
.get("https://jsonplaceholder.typicode.com/posts")
.then((response) => {
setList(response.data);
})
.catch((err) => console.log(err));
}, []);
return (
<div>
<ul>
<div>
{list.map((item, index) => (
<li key={item.id}>
<div>{item.title}</div>
</li>
))}
</div>
</ul>
</div>
);
};
As you can see all the titles are shown. How can I for example show titles of only userID
= 2?
You need to update like this:
{list.map((item, index) => {
if (item.userId === 2) {
return (
<li key={item.id}>
<div>{item.title}</div>
</li>
);
}
return null;
})}
Link: https://codesandbox.io/s/peaceful-oskar-x0r5t?file=/src/App.js
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.