Giter Site home page Giter Site logo

reduxprac's Introduction

Redux

Redux is a state management library, an application data-flow architecture to be specific

Redux construction

Redux uses a store object that store all state required by an application and needed to be passed on to your project components

Important Redux methods

Redux.createStore()

  • The create store method is used to create a store object for your redux program which will be exported and used in other components for state management
  • The createStore(reducer) takes in a reducer as an argument (discussed below)
  • An optional argument passed to the createStore method is the Redux.middleWare() method which is used to handle async functions
    const store = Redux.createStore(reducer)

const reducer = ()=>{}

  • The reducer is a function used in redux that takes in state and actions as parameters and always returns a state(either updated or same if the conditions of the state rendering are met).
  • Most preferrably use a switch(){case} statement for conditional rendering of the state to be passed to the store
    const reducer = (state=defaultState, action)=>{
        switch(action.type){
            case CASE1:
                return state
            default:
                return state
        }
    }

store.dispath()

  • The .dispatch() method is used to throw in actions to the store created using the redux createStore method
  • Takes in an action object type as the parameter or most preferrably takes in a action creator as an argument to make your code simple and more readable
  • Can optionally take in a callback as a second argument, but the callback is most prefarrably placed inside a function which is added as a parameter in the .subscribe(callback) method.
    store.dispatch(action creator)

store.subscribe(callback)

  • A special redux method that adds a listener to a store
  • Determines what a your program should execute immediately after a state is updated in the reducer
  • Takes in a callback function created as an argument

Redux.combineReducer()

  • This redux method combines multiple reducers in a redux file that will be used in the createStore method which can only be called once
  • Takes in an object with key value pairs of reducers as an argument
    const reducers = Redux.combineReducers({
        redu1 : nameOfReducer,
        redu2 : nameOfReducer
    })

store.getState

  • This method is used in getting the current state of a redux store
    const result = store.getState()

reduxprac's People

Contributors

walobwad avatar elizabethmusanga 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.