简体   繁体   English

如何减少 React.js 中条件的使用

[英]How to reduce the use of conditions in React.js

I have a block of codes in React.js which I believe is not the best way to do it.我在 React.js 中有一段代码,我认为这不是最好的方法。 However, I am not sure how I can simplify and optimize it.但是,我不确定如何简化和优化它。 Does anyone have any ideas?有没有人有任何想法? Thanks so much非常感谢

const url = new URL(window.location.href);
let date = "";
let locationId = 0, movieId = 0;

const urlDate = url.searchParams.get("date");
if (urlDate) {
    if (dateSelectors.filter(x => x.code === urlDate).length > 0) date = urlDate;
    else toast.error("Date retrieved from the URL is invalid");
}
const urlMovie = url.searchParams.get("movieId");
if (urlMovie && urlMovie !== "0") {
    if (!Number.isNaN(+urlMovie) && movieSelectors.filter(x => x.code === urlMovie).length > 0) movieId = urlMovie;
    else toast.error("Movie Id retrieved from the URL is invalid");
}
const urlLocation = url.searchParams.get("locationId");
if (urlLocation && urlLocation !== "0") {
    if (!Number.isNaN(+urlLocation) && locationSelectors.filter(x => x.code === urlLocation).length > 0) locationId = urlLocation;
    else toast.error("Theatre Id retrieved from the URL is invalid");
}

This is a very subjective and wide question but here is my suggestion.这是一个非常主观和广泛的问题,但这是我的建议。

Because the last two blocks of code have identical logic, you could make a function to simplify it, like this:因为最后两个代码块具有相同的逻辑,您可以制作一个 function 来简化它,如下所示:

const handleUrls = (url, selector) => {
  if (url && url !== "0") {
    if (
      !Number.isNaN(+url) &&
      selector.filter((x) => x.code === url).length > 0
    )
      locationId = urlLocation;
    else toast.error(`The ${url} URL is invalid`);
  }
};

handleUrls(urlLocation, locationSelectors);
handleUrls(urlMovie, movieSelectors);
const url = new URL(window.location.href);
const getValue = (key)=> url.searchParams.get(`${key}`)
const toastOnError = (message)=> toast.error(`${message}`)
let date = "";
let locationId = 0, movieId = 0;

const urlDate = getValue("date");
if (urlDate) {
    date = (dateSelectors.filter(x => x.code === urlDate).length > 0) ? urlDate : "";
    !date && toastOnError("Date retrieved from the URL is invalid")
}
const urlMovie = getValue("movieId");
if (urlMovie && urlMovie !== "0") {
    movieId = (!Number.isNaN(+urlMovie) && movieSelectors.filter(x => x.code === urlMovie).length > 0) ? urlMovie : movieId;
    !movieId && toastOnError("Movie Id retrieved from the URL is invalid");
}
const urlLocation = getValue("locationId");
if (urlLocation && urlLocation !== "0") {
     locationId  = (!Number.isNaN(+urlLocation) && locationSelectors.filter(x => x.code === urlLocation).length > 0) ? urlLocation: locationId;
     !locationId && toastOnError("Theatre Id retrieved from the URL is invalid");
}

We can create some handy inline functions to avoid duplication and some formating on conditions.我们可以创建一些方便的内联函数来避免重复和某些条件格式。 Further, I don't know exactly what do dateSelectors, movieSelectors etc functions do.此外,我不确切知道 dateSelectors、movieSelectors 等函数的作用。 By optimizing those, you can create a good version of your code.通过优化这些,您可以创建一个好的代码版本。

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

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