#Next.js#React

useActionState 跟 Server Actions 配起来香不香?

由@lin-shen发起讨论 · 发布于 2026年7月31日 · 更新于 2026年8月3日

试了下 useActionState,整体思路很好,但 API 设计有点绕。它要传一个 reducer 风格的函数,返回值是 [state, action, isPending],跟传统 hook 习惯不太一样。

tsx
const [state, formAction, pending] = useActionState(submitFn, initialState);

pending 是后加的,之前还得自己包一个 transition 拿 isPending。现在爽多了。

但错误信息的 TS 类型推导有点弱,得手动标 union,不然 state.error 推不出来。

你们用下来感觉咋样?有没有更好的封装姿势?

回复

暂时没有回复。

Related

相关推荐