Skip to content

fix: use lazy initializer for useState with function calls#3867

Open
karyanayandi wants to merge 1 commit intoDokploy:canaryfrom
karyanayandi:fix/lazy-initializer-for-usestate
Open

fix: use lazy initializer for useState with function calls#3867
karyanayandi wants to merge 1 commit intoDokploy:canaryfrom
karyanayandi:fix/lazy-initializer-for-usestate

Conversation

@karyanayandi
Copy link
Contributor

@karyanayandi karyanayandi commented Mar 3, 2026

What is this PR about?

Calling getTerminalKey() and getDefaultDateRange() directly in useState
re-runs them on every render. Wrap in an arrow function so they execute
only once during component initialization.

Checklist

Before submitting this PR, please make sure that:

  • You created a dedicated branch based on the canary branch.
  • You have read the suggestions in the CONTRIBUTING.md file https://github.com/Dokploy/dokploy/blob/canary/CONTRIBUTING.md#pull-request
  • You have tested this PR in your local instance. If you have not tested it yet, please do so before submitting. This helps avoid wasting maintainers' time reviewing code that has not been verified by you.

Greptile Summary

This PR applies the React useState lazy initializer pattern to two components, wrapping direct function calls (getTerminalKey() and getDefaultDateRange()) in arrow functions so they are only executed once during component initialization rather than on every render.

  • In terminal-modal.tsx, getTerminalKey() (which calls Date.now()) was being evaluated on every render; it is now wrapped as () => getTerminalKey().
  • In show-requests.tsx, getDefaultDateRange() (which creates new Date() objects) was similarly evaluated on every render; it is now wrapped as () => getDefaultDateRange().

Both changes are correct and follow React best practices. The fix prevents unnecessary function evaluations and avoids creating throwaway object allocations on each re-render. No logic changes are introduced — only the initialization timing is corrected.

Confidence Score: 5/5

  • This PR is safe to merge — it applies a well-known React optimization with no behavioral changes.
  • The changes are minimal, correct, and purely a performance micro-optimization. Lazy initializers in useState are idiomatic React; both wrapped functions are side-effect-free (aside from Date.now(), which is intentional and was already the existing behavior). No new logic is introduced and there is no risk of regression.
  • No files require special attention.

Last reviewed commit: 43f9f54

(5/5) You can turn off certain types of comments like style here!

Calling getTerminalKey() and getDefaultDateRange() directly in useState
re-runs them on every render. Wrap in an arrow function so they execute
only once during component initialization.
Copilot AI review requested due to automatic review settings March 3, 2026 01:28
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 3, 2026
@dosubot dosubot bot added the bug Something isn't working label Mar 3, 2026
@dosubot
Copy link

dosubot bot commented Mar 3, 2026

Related Documentation

Checked 7 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates a couple of React useState initializations to use lazy initializer functions, avoiding re-executing initializer function calls on every render and ensuring they only run during the initial mount.

Changes:

  • Wrap getTerminalKey() in a lazy useState initializer in the terminal modal.
  • Wrap getDefaultDateRange() in a lazy useState initializer in the requests dashboard.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
apps/dokploy/components/dashboard/settings/web-server/terminal-modal.tsx Makes terminalKey state initialization lazy so getTerminalKey() only runs on initial mount.
apps/dokploy/components/dashboard/requests/show-requests.tsx Makes dateRange state initialization lazy so getDefaultDateRange() only runs on initial mount.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants