mirror of
https://github.com/Tomato6966/investment-portfolio-simulator.git
synced 2025-04-11 03:40:35 +02:00
18 lines
641 B
TypeScript
18 lines
641 B
TypeScript
import { useContext, useMemo } from "react";
|
|
|
|
import { PortfolioContext, PortfolioContextType } from "../providers/PortfolioProvider";
|
|
|
|
// main way of how to access the context
|
|
const usePortfolio = () => {
|
|
const context = useContext(PortfolioContext);
|
|
if (!context) {
|
|
throw new Error('usePortfolio must be used within a PortfolioProvider');
|
|
}
|
|
return context;
|
|
};
|
|
|
|
// performance optimized way of accessing the context
|
|
export const usePortfolioSelector = <T,>(selector: (state: PortfolioContextType) => T): T => {
|
|
const context = usePortfolio();
|
|
return useMemo(() => selector(context), [selector, context]);
|
|
};
|