重构 React UseEffect 删除重复的 else if

Refactor React UseEffect removing repeating else if

我有以下useEffect

我想重构它,删除所有这些 else if

见代码

useEffect(() => {
    if(country === "WW") {
        setIsDisable(true);
    } else if (country === "GB") {
        setIsDisable(false);
    } else if (country === "DE") {
        setIsDisable(false);
    } else if (country === "FR") {
        setIsDisable(false);
    } else if (country === "BR") {
        setIsDisable(false);
    } else if (country === "US") {
        setIsDisable(false);
    } else if (country === "IT") {
        setIsDisable(false);
    } else if (country === "US") {
        setIsDisable(false);
    } else if (country === "ES") {
        setIsDisable(false);
    } else if (country === "MX") {
        setIsDisable(false);
    }
  }, [country]);

如你所见国家:GB, DE, FR, BR, US, IT, USESMX 共享相同的 setIsDisable(false)

您将如何编写 1 行代码而不重复相同的代码 else if

useEffect(() => {
   setIsDisable(country === "WW");
}, [country]);

如果你想验证

useEffect(() => {
   if (country === "WW")
      setIsDisable(true);
   else if (["GB", "DE", "FR", "BR", "US", "IT", "ES", "MX"].includes(country))
      setIsDisable(false);
}, [country]);