GithubHelp home page GithubHelp logo

reduxswift's Introduction

ReduxSwift

A redux implementation in Swift

Usage

Creating reducers

func countReducer(previousState: Any, action: Action) -> Int {
    var count = previousState as! Int;
    
    switch action.type {
    case "Increment":
        count += 1
        break
    case "Decrement":
        count -= 1
        break
    default:
        break
    }
    
    return count
}

func nameReducer(previousState: Any, action: Action) -> String {
    var name = previousState as! String;        
    
    switch action.type {
    case "ChangeName":
        name = action.payload as! String
        break
    default:
        break
    }        
    
    return name
}

// Create the combined reducer (If you have multiple reducers)
let combinedReducer = Redux.combineReducers([
    "count": countReducer,
    "name": nameReducer,
])

Creating stores

import ReduxSwift

// Create the initial state
let initialState = [
    "count": 0,
    "name": "Jon Snow"
]

// Create the store
let store = Redux.createStore(
    initialState, 
    reducer: combinedReducer
)

Dispatching Actions

// Initial state
let appState = store.getState() as! [String: Any]
print(appState["count"]) // 0
print(appState["name"]) // Jon Snow

// Dispatch an action to change the name
store.dispatch(action: Action(payload: "Tyrion Lannister", type: "ChangeName"))
appState = store.getState() as! [String: Any]
print(appState["name"]) // Tyrion Lannister

// Dispatch an action to increment the count
store.dispatch(action: Action(payload: nil, type: "Increment"))
appState = store.getState() as! [String: Any]
print(appState["count"]) // 1

// Dispatch an action to decrement the count
store.dispatch(action: Action(payload: nil, type: "Decrement"))
appState = store.getState() as! [String: Any]
print(appState["count"]) // 0

Subscribing/Unsubscribing to the store

// Subscribe to store changes
let unsubscribe = store.subscribe() {
    // handle store changes here
}

// Unsubscribe to store changes
unsubscribe()

Creating & applying middleware

// Create the middleware function

func loggingMiddleware(store: MiddlewareStore) -> DispatchFunction {
    return { (next: Dispatch) in
        return { (action: Action) in
            print("---")
            print("prev state: \(store.getState())")
            print("action: \(action)")
            let returnValue = try next(action: action)
            print("next state: \(store.getState())")
            print("---")
            return returnValue
        }
    }
}

// Apply the middleware

let storeEnhancer = Redux.applyMiddleware([loggingMiddleware])
let storeMaker = storeEnhancer(Redux.createStore)

// Create the store from the created `storeMaker` function

let store = storeMaker(
    initialState: initialState(), 
    reducer: combinedReducer
)

// Use the store

store.dispatch(action: Action(payload: nil, type: "Increment"))
// Output in console: 
//
// ---
// prev state: ["count": 0, "name": "Jon Snow"]
// action: ["type": Increment, "payload": nil]
// next state: ["count": 1, "name": "Jon Snow"]
// ---

reduxswift's People

Contributors

thunderousninja avatar

Watchers

James Cloos avatar  avatar

reduxswift's Issues

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.