question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useStoreState Performance with transform

See original GitHub issue

Hello dear react flow team, we recently had a hard time to overcome some performance issues while using useStoreState to get the transform value. Since we have a lot of sub-components which rely on our custom hooks that sometimes need to use the transform from react-flow to achieve our goals, we needed to split our hooks in terms of whether they used the store state to get the transform property. One of our problems was, that if a component needs data about the zoom level (transform[2]) we still got re-renderings since if transform changes, it also changes if we only need to use the zoom. So we created a custom hook that subscribes to transform with useStoreState and memoized the single values of transform(transform[0], transform[1], transform[2]), that helped a lot.

The second case which is still untackled and could be optimised would be the selector for transform. Of course useStoreState returns a new value whenever the tree is zoomed or moved. Since we need that for calculations when the user moves the tree around that is fine. But yet a delay of receiving the data would be great to improve performance a lot. Even if it is 0.1 seconds.

The two ideas to create a better performance in my opinion with react flow would be the following:

  • Create a hook or a selector that only exports one memoized instance of transform
  • Have the ability to receive the transform value after a defined timeout of seconds when the values has not been changed anymore (delayed callback)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dominikspiertzcommented, Nov 17, 2021

Hello @moklick, no problem. I will create one tomorrow and send it here so you can see it

0reactions
moklickcommented, Mar 15, 2022

Hey @dominikspiertz

we just released v10, that doesn’t use Redux anymore but Zustand. With Zustand it’s possible to do transient updates. Maybe it helps here:

import { useStoreApi } from 'react-flow-renderer';

function Component() { 
  const store = useStoreApi(); 
  const tranform = useRef(store.getState().tranform)
 
 useEffect(() => store.subscribe(
    state => (tranform.current = state.tranform)
  ), [])

 // ... do something with transform.current
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate to v10 Guide - React Flow
This creates a new object on every render and leads to bugs and performance issues: ... const transform = useStoreState((store) => store.transform); ...
Read more >
useStoreState - Easy Peasy v5
The useStoreState hook runs a performance optimisation where it checks to see if mapped state has changed when your store has updated.
Read more >
Easy-Peasy State Management Using React Easy Peasy
useStoreState : This is a custom hook from Easy Peasy that gives our components access to the application's store state. useStoreActions: Like ...
Read more >
React State Management Using Easy Peasy
Easy Peasy was built in mind to provide the same performance as Redux ... useStoreState( (state) => state.conversion.data.amount ); const ...
Read more >
Idiomatic Redux: The History and Implementation of React ...
It's not well documented, but React has a particular performance ... (This is the basis for @babel/plugin-transform-react-constant-elements, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found