anish000kumar / redux-box

Modular and easy-to-grasp redux based state management, with least boilerplate
MIT License
708 stars 22 forks source link

Include getters #14

Closed joshua1 closed 6 years ago

joshua1 commented 6 years ago

like in vuex getters enable you write computed functions that return values that can be used across you app. this values are updated when state changes across your apps.

joshua1 commented 6 years ago

in the example i did this modification to the state

export default {
  posts: [],
  active_post: null,
  postLength(){
    return this.posts.length
  }
}

and in the component used postModule.postLength() and got the correct values updated each time. should this suffice in the absence of a getter property in the module. instead of having this implemented across component, i wanted a reactive feature that is available to all components

anish000kumar commented 6 years ago

@joshua1 that would work.But it's not the recommended way of getting computed values. Because you would end up performing all operations to get your computed properties, every time any item in your module changes (even if it's not the item on which your computed property relies). It becomes very 'performance-expensive' specially when your computed properties rely on multiple values of store, and needs to do some heavy computations like filtering through a long list, sorting long list etc.

So, while it might be okay to use above approach for simple scenarios like the length of posts etc, it's better to stick to the memoization approach for most of the computed properties. You would want to check out https://github.com/reactjs/reselect for better reference. I am working to include redux-reselect or similar technique to ease adding performant computed properties in the module.

joshua1 commented 6 years ago

@anish000kumar thanks for the response. i thought about the performance implications too. Redux-reselect would be a cool addition. i will keep watch for that