Skip to content

refactor(page-heading): Use useEffect for setting page titles#58

Merged
Its4Nik merged 2 commits intodevfrom
fix-Cannot-update-a-component-("PageHeadingProvider")-while-rendering-a-different-component
Jan 8, 2026

Hidden character warning

The head ref may contain hidden characters: "fix-Cannot-update-a-component-("PageHeadingProvider")-while-rendering-a-different-component"
Merged

refactor(page-heading): Use useEffect for setting page titles#58
Its4Nik merged 2 commits intodevfrom
fix-Cannot-update-a-component-("PageHeadingProvider")-while-rendering-a-different-component

Conversation

@Its4Nik
Copy link
Copy Markdown
Owner

@Its4Nik Its4Nik commented Jan 8, 2026

Moved the setHeading calls from PageHeadingContext into useEffect hooks across various pages.

Previously, setHeading was invoked directly during the component's render phase. While often appearing to work, this can lead to subtle bugs, unnecessary re-renders, or warnings in strict mode, as it is a side effect.

By wrapping setHeading in useEffect, we ensure that:

  • The heading is set only after the component mounts.
  • It re-sets only when its dependencies (e.g., setHeading function itself, or the derived heading variable in pluginId.tsx) change.
  • This aligns with React's best practices for managing side effects, improving component stability and preventing potential issues with render cycles.

Summary by Sourcery

Enhancements:

  • Move PageHeadingContext setHeading calls in multiple pages into useEffect hooks so headings are applied after mount and update only when their dependencies change.

Moved the `setHeading` calls from `PageHeadingContext` into `useEffect` hooks across various pages.

Previously, `setHeading` was invoked directly during the component's render phase. While often appearing to work, this can lead to subtle bugs, unnecessary re-renders, or warnings in strict mode, as it is a side effect.

By wrapping `setHeading` in `useEffect`, we ensure that:
- The heading is set only after the component mounts.
- It re-sets only when its dependencies (e.g., `setHeading` function itself, or the derived `heading` variable in `pluginId.tsx`) change.
- This aligns with React's best practices for managing side effects, improving component stability and preventing potential issues with render cycles.
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jan 8, 2026

Reviewer's Guide

Refactors all usages of PageHeadingContext so that page titles are set inside React useEffect hooks instead of during render, aligning heading updates with React side-effect best practices.

Sequence diagram for setting page headings via useEffect

sequenceDiagram
  actor User
  participant Router
  participant React
  participant PageComponent
  participant PageHeadingContext

  User->>Router: Navigate to route
  Router->>React: Render PageComponent
  React->>PageComponent: Invoke function component
  PageComponent->>PageHeadingContext: useContext(PageHeadingContext)
  PageComponent->>PageComponent: Compute heading value
  React-->>PageComponent: Commit rendered UI

  React->>PageComponent: Run useEffect after mount
  PageComponent->>PageHeadingContext: setHeading(heading)
  PageHeadingContext->>PageHeadingContext: Update heading state
  PageHeadingContext-->>React: Trigger subscribed components re-render
  React->>PageComponent: Re-render with updated heading (no new side effects)

  Note over React,PageComponent: setHeading is now a post-render side effect, avoiding side effects during render
Loading

File-Level Changes

Change Details Files
Move heading updates from render phase into useEffect across affected pages.
  • In pluginId page, extract setHeading from PageHeadingContext, compute a heading variable from route metadata or fallback path, and call setHeading inside a useEffect that depends on setHeading and heading.
  • In index (home) page, obtain setHeading from PageHeadingContext and invoke it inside a useEffect to set the heading to 'Home'.
  • In clients page, obtain setHeading from PageHeadingContext and invoke it inside a useEffect to set the heading to 'Clients & Workers'.
  • In extensions/plugins page, obtain setHeading from PageHeadingContext and invoke it inside a useEffect to set the heading to 'Plugin Browser'.
apps/dockstat/src/pages/pluginId.tsx
apps/dockstat/src/pages/index.tsx
apps/dockstat/src/pages/clients/index.tsx
apps/dockstat/src/pages/extensions/plugins.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • There’s now a repeated pattern of const { setHeading } = useContext(PageHeadingContext) + useEffect(() => setHeading(...), [setHeading, ...]) across multiple pages; consider extracting a small helper hook (e.g. usePageHeading(title: string)) to reduce duplication and keep heading logic centralized.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- There’s now a repeated pattern of `const { setHeading } = useContext(PageHeadingContext)` + `useEffect(() => setHeading(...), [setHeading, ...])` across multiple pages; consider extracting a small helper hook (e.g. `usePageHeading(title: string)`) to reduce duplication and keep heading logic centralized.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

The `usePageHeading` hook is introduced to abstract away the `useContext(PageHeadingContext)` and `useEffect` pattern used for setting page titles. This simplifies page components by replacing boilerplate code with a single hook call, improving maintainability and readability.

The change also includes:
- Removal of the `useCurrentLocation` hook from `locations.ts` as it's no longer used.
- Cleanup of unused `CardBody` and `CardHeader` imports in `HostsList` and `PoolStatsCard` components.
@Its4Nik Its4Nik merged commit 6e89df8 into dev Jan 8, 2026
3 checks passed
@Its4Nik Its4Nik deleted the fix-Cannot-update-a-component-("PageHeadingProvider")-while-rendering-a-different-component branch January 8, 2026 21:45
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.

1 participant