From 6a9fa60a1b75a8a11a8c6ca65ca95e8fe43a52c0 Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 11 Nov 2021 12:21:54 +0000 Subject: [PATCH 1/3] fix bug related to caching --- src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 5fceab1..195eecf 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -109,7 +109,7 @@ const useVariant = ({ error: string | null; } => { const [resultIndex, setResultIndex] = useState( - cacheResult ? (Number(getVariantFromStorage(id)) ? Number(getVariantFromStorage(id)) : -1) : -1, + cacheResult ? (Number(getVariantFromStorage(id)) !== NaN ? Number(getVariantFromStorage(id)) : -1) : -1, ); const [error, setError] = useState(null); From 4cde2bdc7d091f8b795848b0969f868e4de5265d Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 23 Nov 2021 13:24:54 +0000 Subject: [PATCH 2/3] [FIX] fix null value conversion to 0 --- src/index.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 195eecf..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)) !== NaN ? 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); From b25cbdda109eb1da654170a8f06b329033ac7551 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 23 Nov 2021 13:26:03 +0000 Subject: [PATCH 3/3] bump version to 1.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",