Skip to content

fix: carousel timer no longer calls parent state update inside a setState updater - #616

Open
zjncs wants to merge 1 commit into
oss-compass:mainfrom
zjncs:fix/academe-timer-purity
Open

zjncs wants to merge 1 commit into
oss-compass:mainfrom
zjncs:fix/academe-timer-purity

Conversation

@zjncs

@zjncs zjncs commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Problem

Timer.tsx:15-30 calls setActiveFun() (parent state update) inside a setProgress updater:

setProgress((prevProgress) => {
  if (prevProgress >= 101) {
    setActiveFun();  // ← parent state update inside an updater
    return 0;
  }
  ...
});

Two React-contract violations:

  1. State updaters must be pure — React may invoke them twice (StrictMode, interrupted/replayed concurrent renders), double-firing the tab advance. Currently masked by reactStrictMode: false and an idempotent next-name computation; any future non-idempotent change (e.g., advancing an index) double-skips tabs.
  2. setActiveFun is a fresh inline closure every parent render and sits in the effect deps, so the interval is torn down/recreated on each parent re-render.

Fix

  • Keep the updater pure (advance/wrap only)
  • Detect completion in a separate useEffect on progress
  • Hold setActiveFun in a ref to stop the interval churn

Verification

Component structure verified: the updater now only computes the next progress value; the advance callback fires from a dedicated effect exactly once per cycle completion.

Risk

None — behavior is identical for the current (StrictMode-off, idempotent) configuration; the fix makes it correct for the general case.

setProgress 的 updater 函数内嵌 setActiveFun()(父 state 更新)——
React 要求 updater 必须是纯函数:StrictMode 与中断/重放的并发渲染
可能重复调用 updater,导致轮播跳标签双触发(当前被 next.config.js
的 reactStrictMode: false 和幂等的 next-name 计算掩盖,任何未来
改为索引推进的修改都会双跳)。

改为纯 updater(推进/归零)+ 独立 effect 在 progress >= 101 时
调用 activeFunRef.current()。setActiveFun 存 ref:父组件传的 inline
闭包每次渲染都是新引用,放在 effect deps 里导致 interval 频繁重建。
Copilot AI balanced review requested due to automatic review settings October 22, 2026 14:08
@vercel

vercel Bot commented Oct 22, 2026

Copy link
Copy Markdown

@zjncs is attempting to deploy a commit to the codersett's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants