Memoize connect response callback functions
#1181
sdemjanenko
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
|
Hi @sdemjanenko. For this use case, I'd recommend using const api = useMemo(() => fileUpload.connect(state, send, normalizeProps), [state])Another approach is to hold the const apiRef = useRef()
const api = fileUpload.connect(state, send, normalizeProps)
apiRef.current = api
useEffect(() => {
apiRef.current.setFiles(value)
}, [value])You could combine both approaches for slightly better results. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I ran into this issue when using the
file-uploadpackage, but I think it applies to all components. The code examplesreturns a new
apiobject every time a component renders. Ideally this would be memoized so child components can optimize their rendering if they so choose. Memoizing the callback functions separately would also be useful. I ran into an issue where I had code that updated the machine's state if a parent component provided a newvalueBecause
api.setFilesis new every time the component renders, theuseEffectgets run (even when the value is unchanged).I think adding memoization into the
connectmethods would be really helpful. This probably means theconnectmethods need to become hooks in React.Beta Was this translation helpful? Give feedback.
All reactions