statsig-io / react-sdk

An SDK for using Statsig Feature Management and Experimentation platform in React js clients
ISC License
6 stars 6 forks source link

useConfig has unstable results #19

Closed samsch closed 6 months ago

samsch commented 7 months ago

useConfig called two times in a row should result in the same reference being returned.

What's expected:

const { config: config1 } = useConfig("config_name");
const { config: config2 } = useConfig("config_name");

config1 === config2 // true

What's actually happening is it's always false. useConfig passes a new object to useConfigImpl with every call that breaks the memoization of useConfigImpl, but also tried importing and calling useConfigImpl directly and that still resulted in different objects returned for each call.

tore-statsig commented 6 months ago

Yup, this is a bug and the root cause is exactly as you identified. We will patch this shortly

tore-statsig commented 6 months ago

Actually, I think this won't quite work even with the fix. The result of that check can be memoize across multiple renders (thats the bug), but back to back calls won't be memoized/give you the same result. You really shouldnt need back to back calls though, just the memoization on future renders