Using useEvaluation as a feature flag in React, how do I handle failures safely?
I'm gating a new checkout flow behind useEvaluation in a React SPA. Two questions before this goes live:
- What should the component render while the query is still resolving?
- If the evaluation call fails at runtime (network, whatever), what does the hook do? I need a guarantee that users land on the stable checkout, never a broken in-between state.
The query itself is simple, something like user is returning. Just want the failure modes nailed down first
1 answer
The pattern you want is "failure means false", and the SDK is designed to make that easy.
For the imperative API the documented approach is:
croct.evaluate('user is returning').catch(() => false);Mirror the same idea with the hook: pass an initial value of false so the component renders the stable path immediately instead of suspending, and treat any error state as false too. Your users only ever see the new checkout once the evaluation has positively resolved to true.
Two things that help here:
- Undefined variables in CQL evaluate to false by design. So if you later gate on a custom attribute that has not been set for a user yet, the flag simply stays off. No error, no crash.
- Because the flag defaults to the stable path, a failed evaluation is indistinguishable from "not in the audience", which is exactly the degraded behavior you want for a checkout.
One caveat: do not cache the result across sessions yourself. Audiences are evaluated in real time, so user is returning can flip mid-session as the profile updates.
The "undefined evaluates to false" detail is exactly the guarantee I needed. Shipped it with initial: false and the rollout has been clean. Thanks.