P粉4208682942023-09-04 13:44:33
Typically, useMemo
is used to cache expensively calculated values during rendering. In your case, however, you don't have any expensive rendering calculations; you just render a very large tree every time the input changes. In fact, because all state is on the App
component, the entire app is re-rendered every time.
The way to optimize this problem in React is to skip rendering components as much as possible. To do this, split unrelated parts of the page into different components. Once the logic is separated, wrap it with React.memo()
, which is a different optimization technique that can skip component rendering entirely.
To me, the most obvious changes you can make are:
TodosDatos
out of the App
component as it is a constant and does not need to be redefined on every render (which may take up memory). <Table>
into a new component and use React.memo()
to memoize it. Make sure to pass all table dependency values to the new component's props. I implemented these changes here: https://codesandbox.io/s/green-breeze-mmum6n?file=/src/App.js. You should now notice that typing is almost instantaneous. You can also do multiple optimizations elsewhere to get better performance.