digitransit-ui/app/configurations/ConfigContext.js
Simo Partinen b4e61ecbd9 feat: added ConfigContext and associated hook
(cherry picked from commit 088052f4a5)
2026-01-22 15:41:07 +02:00

24 lines
643 B
JavaScript

import React, { createContext, useContext } from 'react';
import { PropTypes } from 'prop-types';
import { configShape } from '../util/shapes';
const ConfigContext = createContext();
export function ConfigProvider({ value, children }) {
return (
<ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>
);
}
export function useConfigContext() {
const context = useContext(ConfigContext);
if (!context) {
throw new Error('useConfigContext must be used within a ConfigProvider');
}
return context;
}
ConfigProvider.propTypes = {
value: configShape.isRequired,
children: PropTypes.node.isRequired,
};