1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WithViewStoreに 2 つ以上のstateをobserveする方法

Last updated at Posted at 2023-10-19

概要

TCAを使って画面遷移以外のstateを監視したくないが
画面遷移の処理を複数のstateによって制御したい
そんな場合にどのように解決すればいいのかを調べました。
結論として、WithViewStoreのobserveにViewStateを渡してあげればいいようです。

そもそも、observeってどんな時に使うの? って方は
僕の説明よりもわかりやすい記事がありますので、以下を御覧ください。
https://zenn.dev/kalupas226/articles/c4128a0f99d07b

実装

let store: StoreOf<Test>

private struct ViewState: Equatable {
    let selectedTab: Tab
    var count: Int

    init(state: Test.State) {
        selectedTab = state.selectedTab
        _showsSearchView = state.$count
    }
}

var body: some View {
        WithViewStore(store, observe: ViewState.init) { viewStore in
// ...

BindingState を使用する方法

let store: StoreOf<Test>

private struct ViewState: Equatable {
    let selectedTab: Tab
    @BindingViewState var count: Int

    init(state: BindingViewStore<Test.State>) {
        selectedTab = state.selectedTab
        _showsSearchView = state.$count
    }
}

var body: some View {
        WithViewStore(store, observe: ViewState.init) { viewStore in
// ...

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?