reactjs - How do I access Redux items using selectors after I've normalized my store? -


i'm bit confused on how i'm supposed use selectors after i've normalized redux store.

i have following setup store:

const default_state = {   allid: [],   locations: {} }; 

with following reducer:

handleactions({   ['update']: (state, action) => {     let newid = state.allid.length;      const allid = [...state.allid, newid];     const locations = {       ...state.locations,       [newid]: action.payload     };     return {allid, locations};   } }), ... 

i figured want component:

function mapstatetoprops(state) {   return {     share: callmyselector(state)   }; } 

but don't see how selector except return location associated recent id. i'm thinking normalizing not great here - because wouldn't end searching id in regular case.

the power of selectors moves filtering logic away component consuming data, , away actions/reducer reusable functions. mentioned getting recent location. update logic in reducer, we'd make selector grabs last item.

function selectlatestlocation(state) {   const latestid = state.allids[state.allids.length - 1];    return state.locations[latestid]; } 

this assumes location data structured location id key.

 {    1: { id: 1, name: "usa" },    2: { id: 2, name: "europe" }  } 

in case, normalizing data isn't doing much. let's requirements change, , want europe locations. have state property called europeids contains europe location ids.

function selecteuropelocations(state) {    return state.europeids.map(id => state.locations[id]); } 

using selectors normalized redux state make easy change how data filtered. said, cases might not need normalized. project, , being accomplished. but, it's worth data needs memoized, or filtered in different ways!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -