diff --git a/src/hooks/useTreatment.js b/src/hooks/useTreatment.js new file mode 100644 index 000000000..8b71286f6 --- /dev/null +++ b/src/hooks/useTreatment.js @@ -0,0 +1,20 @@ +import { useMemo } from 'react'; +import { useTreatments } from '@splitsoftware/splitio-react'; + +const useTreatment = (name) => { + const treatments = useTreatments([name]); + const { treatment, config } = treatments[name]; + + return { + treatment, + config: useMemo(() => { + try { + return JSON.parse(config); + } catch (e) { + return {}; + } + }, [config]), + }; +}; + +export default useTreatment;