简体   繁体   English

React.js 组件中的多个 if 语句

[英]multiple if statements in a React.js component

I am working on my portfolio and I bumped into this problem.我正在研究我的投资组合,我遇到了这个问题。 This component gets tasks(array) and sortBy(either 'all', 'true', or 'false').该组件获取任务(数组)和 sortBy('all'、'true' 或 'false')。 The problem is, even though I pass 'all' value of sortBy, the second if statement gets called.问题是,即使我传递了 sortBy 的“所有”值,第二个 if 语句也会被调用。

const TaskListTable = ({ tasks, sortBy }) => {
  let filteredTasks;
  let sortByBoolean;
  if (sortBy === 'all') {
    filteredTasks = tasks;
  }
  if (sortBy === 'true' || 'false') {
    sortByBoolean = sortBy === 'true';
    filteredTasks = tasks.filter((task) => task.completed === sortByBoolean);
  }
  console.log(sortBy);
  console.log(sortByBoolean);
  console.log(filteredTasks);

  const withTasks = (
    <div className='task-table'>
      <UtilButton purpose='add' pushUrl='/task-add' />
      <div className='task-table-head'>
        <div>Date</div>
        <div>Task</div>
        <div>Status</div>
        <div></div>
      </div>
      {filteredTasks.map(({ _id, ...others }) => (
        <TaskList key={_id} id={_id} {...others} />
      ))}
    </div>
  );
  const withoutTasks = <UtilPage purpose='emptyData' />;

  return <Fragment>{tasks.length > 0 ? withTasks : withoutTasks}</Fragment>;
};

I solved this problem with this code below instead of using 2 if statements.我用下面的代码解决了这个问题,而不是使用 2 个 if 语句。 But I'd still want to know why the code above with 2 if statements don't work.但我仍然想知道为什么上面带有 2 个 if 语句的代码不起作用。

const sortByBoolean = sortBy === 'true';
const filteredTasks = sortBy !== 'all' ? tasks.filter((task) => task.completed === sortByBoolean) : tasks;

thank you in advance先感谢您

This part...这部分...

if (sortBy === 'true' || 'false') {
  //...
}

...should be... ...应该...

if (sortBy === 'true' || sortBy === 'false') {
  //...
}

You didn't check the condition if sortBy equals 'false' but you checked the value 'false' itself.如果sortBy等于'false' ,您没有检查条件,但您检查了值'false'本身。 This will return true because the string 'false' is truthy .这将返回true因为字符串'false'真实的。
So currently both of your if statements are true and are executed.所以目前你的两个 if 语句都是真的并且被执行。

If you write two If statements consecutively then definately it will execute both of this two .如果您连续编写两个If statements ,那么它肯定会同时执行两个 This is the behaviour of If statement .这是If statement的行为。

So you may used if else if method , also write ||所以你可以用if else if method ,也写|| statement separately to solve this problem.单独声明来解决这个问题。

   if (sortBy === 'all') {
    filteredTasks = tasks;
  }
  else if (sortBy === 'true' || sortBy === 'false') {
    sortByBoolean = sortBy === 'true';
    filteredTasks = tasks.filter((task) => task.completed === sortByBoolean);
  }

because your if condition is not as expected because both if will be executed to skip second if when first is passed add else因为您的 if 条件与预期不符,因为如果通过了第一个,则两个 if 都将被执行以跳过第二个添加 else

if (sortBy === 'all') {
  filteredTasks = tasks;
} else if (sortBy === 'true' || sortBy === 'false') {
  sortByBoolean = sortBy === 'true';
  filteredTasks = tasks.filter((task) => task.completed === sortByBoolean);
}

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

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