Reduck has already done a lot of performance optimization work internally, so performance issues generally do not need to be considered. However, when performance is more sensitive, or when encountering performance issues, you can consider more targeted performance optimization from the following three aspects.
When useModel returns the complete State object of the Model, any change in any part of the State will cause the component that calls useModel to be re-rendered.
For example:
Although ComponentA only needs to use the a state, it will still be re-rendered when the b state changes. In this case, we can consider splitting fooModel into separate Models responsible for managing a and b respectively:
useModel supports passing in a selector function to filter the returned State and Actions for the component. We can use a selector function to ensure that the State returned to the component is what the component needs directly, thus ensuring that the component is not re-rendered due to changes in other unrelated states.
For the same example above, we can use a selector function for performance optimization, the code is as follows:
When a Model has computed property, the computed function will be executed every time useModel is called.
Consider the following code:
Even if the b state of fooModel changes, the combineA function (more precisely, the last function type element of combineA) will still be called and executed when the component is re-rendered, although the derivative state combineA of barModel depends on barModel itself and the state a of fooModel.
In general, the logic in the computed function is usually very lightweight, but when the logic in the computed function is relatively complex, we can consider caching the calculation logic. For example, we can use reselect to cache combineA of barModel:
We created a caching function createSelector, which only recalculates the value of combineA when the state of barModel changes or the state a of fooModel changes.
You can find the complete example code of this section here.