Open neriyarden opened 1 month ago
Hi @neriyarden,
Yes, that behavior (client
is null
when isReady
is still false
) is expected for the SplitFactoryProvider
component. This is a breaking change compared to the now deprecated SplitFactory
component.
In summary, the client
property is null during the first render to avoid side-effects and follow the "rules" of React components. You can read more about it in the following references:
You don't necessarily have to lazy load the SplitClient or your component, but you should conditionally render based on the value of the isReady
property (or isReadyFromCache
). For example:
import { useSplitClient } from "@splitsoftware/splitio-react";
const MySplitClientChildrenComponent = (props) => {
const { isReady, isReadyFromCache, client } = useSplitClient();
return isReady || isReadyFromCache ?
<p>`My treatment is ${client.getTreatment(MY_FEATURE_FLAG}`</p> : // client is available
<Loading /> // client is null
}
But you can directly avoid using the client at all, for example:
import { useSplitTreatments } from "@splitsoftware/splitio-react";
const MySplitClientChildrenComponent = (props) => {
const { isReady, isReadyFromCache, treatments } = useSplitTreatments({ names: [MY_FEATURE_FLAG] });
return isReady || isReadyFromCache ?
<p>`My treatment is ${treatments[MY_FEATURE_FLAG].treatment}`</p> :
<Loading />
}
Hope this helps!
Hey, I don't know if this is a widespread issue or just an issue on my end. I'm seeing a big amount of errors in Sentry of this issue:
TypeError _this.update(@splitsoftware+splitio-react@1.12.0_react@18.3.1/node_modules/@splitsoftware/splitio-react/es/SplitClient) Unhandled: null is not an object (evaluating 'n.state.client.lastUpdate')
After searching the web and finding nothing about this issue, I dove into the code and found where I think the error occurs:
This is my setup of the SplitClient:
When I log the
isReady
andclient
props i see thatclient
isnull
whenisReady
is still false. Does this mean i have to lazy load the SplitClient and my entire component tree that passes aschildren
(render only whenisReady === true
)?Or am I missing something in my setup?