简体   繁体   中英

How to dispatch an async action with parameters(payload) with ReSwift

How do we dispatch an async action with ReSwift with parameters?

I have created this async action:

func searchItems(state: AppState, store: Store<AppState>) -> Action? {
    var items = [Artwork]()
    HttpHelper.post(url: serverApi.search.rawValue, params: ["term": ""]) { data in
        if let data = data as? [[String: Any]] {
            for row in data {
                items.append(Artwork(data: row))
            }
            DispatchQueue.main.async {
                store.dispatch(UpdateItems(items: items))
            }
        }
    }
    return nil
}

To dispatch an action, I used

store.dispatch(searchItems)

Yet I do not know how to attach the search terms with the action.

Figured it out. Here is an example:

import ReSwift

struct GetShowroom: Action {

    let code: String

    init(code: String) {
        self.code = code
        getIt()
    }

    func getIt() {
        store.dispatch(UpdateLoading(loading: .Showroom(code: self.code)))
        HttpHelper.post(to: .getShowroom, params: ["code": self.code]) { res in
            if let data = res as? [String: Any] {
                store.dispatch(UpdateShowroom(showroom: Showroom(code: self.code, data: data)))
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM