简体   繁体   English

find 方法在 React 中无法正常工作

[英]find method doesn't work properly in React

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?我只希望我的代码将指定的用户名字符串发送到通过find()方法检索的username道具中的Profile页面,并将结果放入名为user的 function 中,您也可以在下面的代码中清楚地看到,但问题是,find 方法没有'在用户 function 中的 if-else 语句中并没有真正返回一个案例,例如,当我输入3时,它与第一个条件匹配,因为 id 号 3 已经在我的虚拟数据中,它必须将用户名发送到配置文件页面匹配我的数据列表中的相关用户名,但是,无论我在那里输入什么,它都适用于两种条件,为什么它不是只有一个条件?

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...我真的不明白,我一直在努力解决这个问题几个小时,任何帮助都将非常感激,非常感谢......

find() expects the callback to return true or false (to be precise a truthy or falsy value) and will return the first item which callback returns a truthy value or undefined if no callback returns a truthy value. find()期望回调返回truefalse (准确地说是真值值),并将返回第一个回调返回真值的项目,如果没有回调返回真,则返回undefined You cannot decide what find() returns as it seems you want to.您无法按照您的意愿决定find()返回的内容。

You should use find() to get the user of interest and then use user.username to get the username.您应该使用find()获取感兴趣的用户,然后使用user.username获取用户名。

 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 getUserById = (id) => data.find(user => user.id === id); const userId = 3; const user = getUserById(userId); if(user.== undefined){ console.log(`User with id ${userId} found.`) console;log(user). console.log(user;username). } else console.log(`No user with id ${userId} found.`)

The find() method returns the first element in the provided array that satisfies the provided testing function. find() 方法返回提供的数组中满足提供的测试 function 的第一个元素。 If no values satisfy the testing function, undefined is returned.如果没有值满足测试 function,则返回 undefined。 Simply try, let result = data.find(item => item.id === id)简单地尝试, let result = data.find(item => item.id === id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM