你不需要总是在 React 中使用 useState

在我审查的一个拉取请求中,我注意到在许多拉取请求中看到的一种模式。React 组件具有多个 UI 状态,例如 loadingerrorsuccess

作者使用了多个 useState 钩子来管理这些状态,这导致代码难以阅读且容易出错,例如:

js 复制代码
const MyComponent = () => {
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState(false)
  const [success, setSuccess] = useState(false)

  return (
    <div>
      {loading && !error && !success && <p>Loading...</p>}
      {error && !loading && !success && <p>Error occurred</p>}
      {success && !loading && !error && <p>Operation completed successfully</p>}
    </div>
  )
}

这些状态彼此不同。当 loadingtrue 时,errorsuccess 应该为 false。使用多个 useState 钩子可能会导致意外行为,例如意外 true 同时设置两个状态。

相反,请考虑使用有限状态机(FSM) 模式。FSM 只允许有限数量的状态。在上面的 UI 示例中,单个 useState 可以更稳健地管理当前状态,并且出错的风险更低,如下所示:

js 复制代码
import { useState } from 'react'

type State = 'loading' | 'error' | 'success'

const MyComponent = () => {
  const [state, setState] = useState<State>('loading')

  const handleClick = () => {
    setState('loading')
    // Simulate an async operation
    setTimeout(() => {
      setState('success')
    }, 2000)
  }

  return (
    <div>
      {state === 'loading' && <p>Loading...</p>}
      {state === 'error' && <p>Error occurred</p>}
      {state === 'success' && <p>Operation completed successfully</p>}
      <button onClick={handleClick}>Click me</button>
    </div>
  )
}

在某些情况下,例如使用 Tanstack 查询来获取数据时,useQuery 无需单独 useState 挂钩 来设置 loadingerrorsuccess 状态:

js 复制代码
const MyComponent = () => {
  const { data, isLoading, error } = useQuery(...)

  if (isLoading) {
    return <p>Loading...</p>
  }

  if (error) {
    return <p>Error occurred</p>
  }

  return <p>Operation completed successfully {data}</p>
}

让我们考虑另一个名为 locked 的状态,它根据服务器发送的 403 状态代码显示用户是否已解锁该功能。通常情况下,开发人员可能会使用 useStateuseEffect 来管理该状态,这可能会增加不必要的复杂性:

js 复制代码
const MyComponent = () => {
  const [locked, setLocked] = useState(false)
  const { data, isLoading, error } = useQuery(...)

  useEffect(() => {
    if (error && error.status === 403) {
      setLocked(true)
    }
  }, [error])

  if (locked) {
    return <p>You are locked out</p>
  }
}

更好的方法是直接从 error 中推导出锁定状态:

js 复制代码
const MyComponent = () => {
  const { data, isLoading, error } = useQuery(...)

  if (isLoading) {
    return <p>Loading...</p>
  }

  const locked = error?.status === 403

  if (locked) {
    return <p>You are locked out</p>
  }
}

这种方法可以避免使用 useStateuseEffect 进行额外的状态管理。

在编写 React 组件时,请务必考虑是否有必要使用 useStateuseEffect。通常情况下,它们是不必要的。

相关推荐
工业甲酰苯胺3 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
brzhang3 小时前
我操,终于有人把 AI 大佬们 PUA 程序员的套路给讲明白了!
前端·后端·架构
止观止3 小时前
React虚拟DOM的进化之路
前端·react.js·前端框架·reactjs·react
goms3 小时前
前端项目集成lint-staged
前端·vue·lint-staged
谢尔登4 小时前
【React Natve】NetworkError 和 TouchableOpacity 组件
前端·react.js·前端框架
Lin Hsüeh-ch'in4 小时前
如何彻底禁用 Chrome 自动更新
前端·chrome
augenstern4166 小时前
HTML面试题
前端·html
张可6 小时前
一个KMP/CMP项目的组织结构和集成方式
android·前端·kotlin
G等你下课6 小时前
React 路由懒加载入门:提升首屏性能的第一步
前端·react.js·前端框架
谢尔登7 小时前
【React Native】ScrollView 和 FlatList 组件
javascript·react native·react.js