diff --git a/package.json b/package.json index ed0e252..8871751 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-ab-test-hooks", - "version": "1.0.4", + "version": "1.0.5", "description": "A fast and lightweight AB-Testing library for React and Next.js based on hooks and functional components", "author": "NiklasMencke", "license": "MIT", diff --git a/src/index.tsx b/src/index.tsx index 5fceab1..91a5c5d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -75,6 +75,7 @@ const getWeightedRandomInt = (spec: any): number => { table.push(i); } } + console.log(table, Number(table[Math.floor(Math.random() * table.length)])) return Number(table[Math.floor(Math.random() * table.length)]); }; @@ -109,7 +110,13 @@ const useVariant = ({ error: string | null; } => { const [resultIndex, setResultIndex] = useState( - cacheResult ? (Number(getVariantFromStorage(id)) ? Number(getVariantFromStorage(id)) : -1) : -1, + () => { + if (cacheResult) { + const savedVariant = getVariantFromStorage(id); + if (typeof savedVariant === "string") return Number(getVariantFromStorage(id)); + } + return -1; + } ); const [error, setError] = useState(null);