Skip to content

added useMemo to the Memoize NetworkContext.jsx#48

Open
aniket866 wants to merge 1 commit intoDjedAlliance:mainfrom
aniket866:Memoize-NetworkContext.jsx-with-useMemo
Open

added useMemo to the Memoize NetworkContext.jsx#48
aniket866 wants to merge 1 commit intoDjedAlliance:mainfrom
aniket866:Memoize-NetworkContext.jsx-with-useMemo

Conversation

@aniket866
Copy link

@aniket866 aniket866 commented Jan 23, 2026

Key Effects:

  1. Eliminates Wasted Re-renders: Wrapping the Context value in useMemo ensures that consuming components (like Widget and TransactionReview) only re-render when actual data changes, rather than on every parent render cycle.

  2. Stabilizes Function References: Implementing useCallback for selectNetwork, selectToken, and resetSelections prevents these functions from being recreated on every render, maintaining stable identities for dependency checks.

  3. Standardizes Context Pattern: Aligns the context provider with React best practices for high-frequency updates, reducing the overall CPU and memory footprint of the SDK widget.

Wrap context value in useMemo to maintain referential stability

const contextValue = useMemo(() => ({ 
   networkSelector,
   tokenSelector,
   selectedNetwork,
   selectedToken,
   transactionDetails,
   setTransactionDetails,
   selectNetwork,
   selectToken,
   resetSelections
 }), [
   networkSelector,
   tokenSelector,
   selectedNetwork,
   selectedToken,
   transactionDetails,
   selectNetwork,
   selectToken,
   resetSelections
 ]);

 return (
   // Pass the memoized object instead of creating a new one {{ ... }}
   <NetworkContext.Provider value={contextValue}>
     {children}
   </NetworkContext.Provider>
 );

@Zahnentferner Please checkout this Improvement, If any issue let me know I will fix that . Closes #43

Summary by CodeRabbit

  • New Features

    • Added useNetwork() public hook providing simplified access to network context and state management.
  • Refactor

    • Optimized network context operations to improve performance and application stability.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 23, 2026

📝 Walkthrough

Walkthrough

NetworkContext.jsx received performance optimizations using React hooks: useCallback wrapped four callback functions (resetState, selectNetwork, selectToken, resetSelections) and useMemo wrapped the provider value object to prevent unnecessary re-renders. A new public hook useNetwork() was exported for context access.

Changes

Cohort / File(s) Change Summary
NetworkContext Performance Optimization
stablepay-sdk/src/contexts/NetworkContext.jsx
Added useCallback memoization for state callbacks (resetState, selectNetwork, selectToken, resetSelections); wrapped provider value object with useMemo to ensure referential stability; exported new public hook useNetwork()

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • Zahnentferner

Poem

🐰 A rabbit hops through callbacks memoized,
Where useMemo keeps values organized,
No needless renders shall pass our way,
The NetworkContext is optimized today!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: wrapping NetworkContext with useMemo to memoize the context value.
Linked Issues check ✅ Passed The PR successfully implements all objectives from issue #43: prevents new value object creation on every render via useMemo, stabilizes callbacks with useCallback, and eliminates unnecessary re-renders of consuming components.
Out of Scope Changes check ✅ Passed All changes are scoped to addressing issue #43. The addition of useNetwork() hook is a complementary export enabling easier access to the optimized context.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Network optimization :Missing useMemo for Context Values (NetworkContext.jsx)

1 participant