Git Product home page Git Product logo

game-of-pets's Introduction

Game Of Pets ๐Ÿบ ๐Ÿ‰

As your app grows more complex, you'll want to split your reducing function into separate functions, each managing independent parts or different slices of the state. A popular convention is to name reducers after the state slices they manage, as well as separating them into separate files. For example, if I have one single reducer that handles an e-commerce application I might have a state that looks like this: (items being things available for purchase)

initialState = {
    users: [],
    selectedUser: {},
    items: [],
    selectedItem: {}
}

Using combineReducers allows me to separate my store's state into individual slices, so instead of having one reducer that handles everything about users and items, I can make a user reducer and an items reducer :)

    // action types are omitted for brevity
    // ------ In my UserReducer.js ------
    initialState = {
        users: [], 
        selectedUser: {}
    }

    const reducer = (state = initialState, action) => {
        switch(action.type) {
            case GET_USERS: 
                return {...state, users: action.payload}
            /* More cases can be added here, but omitted for brevity */
            default:
                return state;
        }
    } 
    // action types are omitted for brevity
    // ----- In my ItemsReducer.js -----
    initialState = {
        items: [], 
        selectedItem: {}
    }
    const reducer = (state = initialState, action) => {
        switch(action.type) {
            case GET_ITEMS: 
                return {...state, items: action.payload}
            /* More cases can be added here, but omitted for brevity */
            default:
                return state;
        }
    } 

Link to Redux docs: https://redux.js.org/recipes/structuring-reducers/using-combinereducers

game-of-pets's People

Contributors

julissa93 avatar mariiaromaniuk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.