Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add to docs: Instructions on how to clear the ReSwift state. #367

Open
carstenhag opened this issue Jul 25, 2018 · 6 comments
Open

add to docs: Instructions on how to clear the ReSwift state. #367

carstenhag opened this issue Jul 25, 2018 · 6 comments

Comments

@carstenhag
Copy link

Took me 10 minutes to figure this out, actually, I just had forgotten to call store.dispatch(ClearStore()). I am sleepy, for my defense.

I thought this would help beginners with this lib. Maybe add it to the docs or somewhere? At least with this issue it can be found via search engines.

In your StoresReducer.swift file, add another case.

func storesReducer(action: Action, state: AppState?) -> AppState {
    // if no state has been provided, create the default state
    var state = state ?? AppState()
    switch action {
    case _ as ClearStore:
        state = AppState() // creates a new AppState, like 3 lines earlier
     // [..]
    default:
        break
    }

    return state

To your Actions.swift, add an empty Action:

struct ClearStore: Action { }

Finally, inside your clearing function (Maybe an Action for a debug button) add

store.dispatch(ClearStore())

If your views subscribe to store changes, the view should immediately behave how an initial installation of your app would.

@carstenhag
Copy link
Author

Actually, I'll reopen so a maintainer sees this and could decide if it's worthy of adding to the docs.

@dav92lee
Copy link

Is there an easy way to do this with a nested store?

eg:

func appReducer(action: Action, state: AppState?) -> AppState {
    // if no state has been provided, create the default state
    return AppState(
        accountState: accountReducer(state: state?.accountState, action: action),
        searchState: searchReducer(state: state?.searchState, action: action)
    )
}

@dav92lee
Copy link

dav92lee commented Aug 10, 2018

i've done it with the following code

func appReducer(action: Action, state: AppState?) -> AppState {
    // if no state has been provided, create the default state
    var state = state ?? AppState(
        accountState: AccountState()
        searchState: SearchState()
    )
    switch action {
    case _ as clearStore:
        state = AppState(
            accountState: AccountState()
            searchState: SearchState()
        )
    default:
        state = AppState(
        accountState: accountReducer(state: state?.accountState, action: action),
        searchState: searchReducer(state: state?.searchState, action: action)
        )
    }
    return state
}

@carstenhag
Copy link
Author

Why do you reinit your state on the default case? That doesn't seem that good? (Not sure if it clears or not in your case)

@ranhsd
Copy link

ranhsd commented Nov 13, 2018

In my implementation I dispatch an action whenever I want to reset my state. This action can be dispatched either in deinit, viewWillDisapper or viewDidLoad (or of course anywhere else). When the Reducer get this action it will reset the state and return the initial one.

func chooseBuyerReducer(state: ChooseBuyerState?, action : Action) -> ChooseBuyerState {
    
    var state = state ?? chooseBuyerInitialState()
    
    switch action {
    case _ as ReSwiftInit:
        break
    case _ as ChooseBuyerActions.ResetState:
        state = chooseBuyerInitialState()
    case let action as ChooseBuyerActions.SetItem:
        state.item = action.item        
    default:
        break
    }
    
    return state
    
}

Usually I reset my state in deinit or in view did load (before I subscribe to the store)

@DivineDominion
Copy link
Contributor

@carstenhag @ranhsd @dav92lee Would any of you like to whip up a small chapter for the docs and open a PR? That's the best place for reference code :)

@DivineDominion DivineDominion changed the title Instructions on how to clear the ReSwift state. add to docs: Instructions on how to clear the ReSwift state. Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants