seti-ramesesv1
Version:
Reusable components and context for Next.js apps
48 lines (45 loc) • 1.93 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { createContext, useState, useContext } from 'react';
// "use client";
// import React, { createContext, useContext, useState, type ReactNode } from "react";
// interface ContentContextType {
// content: { [key: string]: React.FC | null };
// setContent: (id: string, component: React.FC) => void;
// }
// const ContentContext = createContext<ContentContextType | undefined>(undefined);
// export const ContentProvider = ({ children }: { children: ReactNode }) => {
// const [content, setContentState] = useState<{ [key: string]: React.FC | null }>({});
// const setContent = (id: string, component: React.FC) => {
// setContentState((prev) => ({ ...prev, [id]: component }));
// };
// return <ContentContext.Provider value={{ content, setContent }}>{children}</ContentContext.Provider>;
// };
// export const useContent = () => {
// const context = useContext(ContentContext);
// if (!context) {
// throw new Error("useContent must be used within a ContentProvider");
// }
// return context;
// };
const ContentContext = createContext(undefined);
const ContentProvider = ({ children }) => {
const [content, setContentState] = useState({});
// ✅ Only update the target panel if the component actually changed
const setContent = (id, component) => {
setContentState((prev) => {
if (prev[id] === component)
return prev; // no change → skip re-render
return { ...prev, [id]: component };
});
};
return jsx(ContentContext.Provider, { value: { content, setContent }, children: children });
};
const useContent = () => {
const context = useContext(ContentContext);
if (!context) {
throw new Error("useContent must be used within a ContentProvider");
}
return context;
};
export { ContentProvider, useContent };
//# sourceMappingURL=ContentContext.js.map