Skip to content

fix(learn): track location.hash reactively instead of freezing it in useState#129

Merged
tknkaa merged 4 commits intomainfrom
copilot/fix-hash-tracking-logic
Apr 14, 2026
Merged

fix(learn): track location.hash reactively instead of freezing it in useState#129
tknkaa merged 4 commits intomainfrom
copilot/fix-hash-tracking-logic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

useState(location.hash) captured the hash once at mount, so navigating between #basic and #local never updated the sidebar's active highlight. SSR/hydration could also produce a mismatch since the initial server-rendered hash is empty.

Changes

  • app/routes/learn.tsx: Replace frozen state with a live derivation from useLocation():
// Before
const [currentHash] = useState(location.hash);

// After
const currentHash = location.hash;
  • Drops the now-unused useState import.
  • When no hash is present (initial page load), no sidebar item is highlighted as active. Active highlighting only applies once the user navigates to #basic or #local.
Original prompt

app/routes/learn.tsx のハッシュ追跡ロジックを修正してください。

背景

現在、以下のように location.hashuseState の初期値として保持しています。

  • const [currentHash] = useState(location.hash);

この実装だと初回レンダー時の値で固定され、#basic から #local への遷移など、ハッシュ変更に追従できません。SSR/ハイドレーション後のUI整合性の観点でも望ましくありません。

変更要件

  1. useState を使った currentHash の保持をやめる。
  2. useLocation() から得られる現在の location.hash を毎レンダー参照する実装にする。
  3. ハッシュが空の場合のデフォルトを #basic とする(既存のコンテンツ構成に合わせる)。
  4. 既存UI(リンク文言、レイアウト、スタイル、挙動)を極力変えない。
  5. 不要になった import があれば整理する。

期待結果

  • #basic / #local の切り替え時に、サイドバーのアクティブ表示が正しく更新される。
  • ビルド/型チェック/テスト(存在する場合)が通る。

リポジトリ: ut-code/hitori-mahjong
対象ファイル: app/routes/learn.tsx

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: これハッシュを追跡するロジック若干怪しくない?SSR的に

Assistant: うん、怪しいです(特にSSR観点で)
今の実装だと useState(location.hash) を初回レンダー時に固定していて、ハッシュ変更に追従しません

何が問題か

  • location.hash はクライアントで変わる値(#basic#local
  • でも const [currentHash] = useState(location.hash); は初期値を一回読むだけ
  • その結果、サイドバーのハイライトが更新されない可能性が高いです

SSRそのもののクラッシュ原因ではないですが、SSR+hydration後の挙動ズレを起こしやすい書き方です。

最小修正

useState をやめて、毎レンダー location.hash を参照するだけでOKです。

const location = useLocation();
const currentHash = location.hash || "#basic";

あとは既存ロジックほぼそのままで動きます。


必要ならこのファイル向けに、NavLink ベースで「active判定をReact Routerに任せる」形まで含めて、差分パッチ形式で提案できます。

User: じゃあ修正するPR出してほしい

Assistant: ありがとうございます!
いまPR作成処理のユーザー承認待ちなので、承認されたらそのまま修正PRを作成して続行します。

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix hash tracking logic in learn routes fix(learn): track location.hash reactively instead of freezing it in useState Apr 13, 2026
Copilot AI requested a review from tknkaa April 13, 2026 15:04
Copy link
Copy Markdown
Contributor

@tknkaa tknkaa left a comment

Choose a reason for hiding this comment

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

最初チュートリアルページに飛んだときは、ハッシュが無い状態であってほしい。

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 13, 2026

最初チュートリアルページに飛んだときは、ハッシュが無い状態であってほしい。

6cf131f|| "#basic" を削除しました。ページ初回表示時(ハッシュなし)はサイドバーのどの項目もアクティブにならず、#basic または #local に遷移したタイミングで対応する項目がハイライトされます。

Copilot AI requested a review from tknkaa April 13, 2026 16:24
@tknkaa tknkaa marked this pull request as ready for review April 14, 2026 00:30
@tknkaa tknkaa merged commit e458c38 into main Apr 14, 2026
1 check passed
@tknkaa tknkaa deleted the copilot/fix-hash-tracking-logic branch April 14, 2026 00:31
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