I just want my code to send specified username strings to Profile page within username props retrieved through find() method and put the result to a function called user as you can also clearly figure in the code below, but the thing is, find method doesn't really return only one case in my if-else statement in user function, for example, when I put 3, which matches the first condition because of id number 3 already is in my dummy data, it must send the username to profile page matching the related username in my data list, however, it works in both conditions regardless of what I put in there, why doesn't it only one condition?
function App() {
const data = [
{ id: 1, username: "jane", password: "1234" },
{ id: 2, username: "jack", password: "1234" },
{ id: 3, username: "john", password: "1234" },
{ id: 4, username: "elizabeth", password: "1234" },
];
const navigate = useNavigate();
const user = (id) => {
data.find((item) => {
if (item.id === id) {
console.log(item.username);
return item.username;
}
else
{ console.log("user not found")}
}
);
};
setTimeout(() => {
user(3); // 3 is the id of the user and it will return the username after 3 seconds
return;
}, 3000);
return (
<div>
<h1>Navbar</h1>
<Link to="/">Home</Link>
<Link to="login">Login</Link>
<Routes>
<Route path="/" element={<Home />} />
<Route path="login" element={<Login />} />
<Route path="users" element={<Profile />}>
<Route path=":id" element={<Profile username={user} />} />
<Route path="*" element={<h1>404 not found</h1>} />
</Route>
<Route path="*" element={<h1>404 not found</h1>} />
</Routes>
</div>
);
}
export default App;
I really don't get this, I'm all stuck trying to fix this for hours and any help will be super appreciated, thanks a lot...