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
-
I ran into this issue when using the
file-upload
package, but I think it applies to all components. The code examplesreturns a new
api
object 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 newvalue
Because
api.setFiles
is new every time the component renders, theuseEffect
gets run (even when the value is unchanged).I think adding memoization into the
connect
methods would be really helpful. This probably means theconnect
methods need to become hooks in React.Beta Was this translation helpful? Give feedback.
All reactions