Closed VitaliyPotapov closed 10 months ago
@VitaliyPotapov it is returning the setData function, but not calling it setData(). So it will not directly re-rendered. And if you are using versions after React 16, calling setState with null also won't trigger an update.
Thanks Karlie! I'd like to clarify: every time I send event I actually call returned setData()
-> state gets updated and from my understanding component will re-render. Is it correct?
I'm thinking why useTrackEvent
is implemented as useState + useEffect
, not just useCallback
.
@VitaliyPotapov Thanks for clarification! I guess you are using useTrackEvent()
like this example
So return setData()
directly for useTrackevent()
could give you more controls on trackEvent data!
Yes I also wrap useTrackEvent
in useCallback
. But my question is about extra re-render that seems to occur even in your example. Please correct me if I'm wrong:
onClick
handler calls setTestNumber(curTestNumber + 1)
trackEvent(testNumber)
that writes to state new testNumber
My question is - could we avoid extra re-render by implementing useCustomEvent
without writing to component state?
For example like this:
export default function useCustomEvent<T>(
reactPlugin: ReactPlugin,
eventName: string,
eventData: T,
): AIReactCustomEvent<T> {
return useCallback(() => {
reactPlugin.trackEvent({ name: eventName }, data);
}, [reactPlugin, data, eventName]);
}
@VitaliyPotapov I thought you were referring to duplicated events are being triggered. 😊And you are correct! useCallBack()
definitely optimizes performance by skipping re-rendering! However, downside of using useCallback
hook is suggested here (correct me if I am wrong)
This Issue will be closed in 30 days. Please remove the "Stale" label or comment to avoid closure with no action.
As useTrackEvent actually returns
setData
fromuseState
- does it mean it will trigger component re-render every time I send event?