Skip to content

side-effects #69

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

Open
LeoAndo opened this issue Oct 24, 2022 · 9 comments
Open

side-effects #69

LeoAndo opened this issue Oct 24, 2022 · 9 comments

Comments

@LeoAndo
Copy link
Owner

LeoAndo commented Oct 24, 2022

@LeoAndo
Copy link
Owner Author

LeoAndo commented Oct 25, 2022

スナックバーを表示するなどの 1 回限りのイベントをトリガーする場合や、特定の状態で別の画面に移動する場合などに、副作用(side-effects)が必要になることがあります。

@LeoAndo
Copy link
Owner Author

LeoAndo commented Oct 25, 2022

LaunchedEffect

コンポーザブル内から suspend 関数を安全に呼び出すには、LaunchedEffectコンポーザブルを使用します。
LaunchedEffect は、いずれかのキーパラメータが変化すると、再起動されます。(blockの中の処理が走る)

LeoAndo pushed a commit that referenced this issue Oct 25, 2022
#69 LaunchedEffect: run suspend functions in the scope of a composable
@LeoAndo
Copy link
Owner Author

LeoAndo commented Oct 25, 2022

rememberCoroutineScope

コンポーザブルの外部でコルーチンを起動するために、Composition から退場すると自動的にキャンセルされるスコープを設定するには、rememberCoroutineScope を使用します。

@LeoAndo
Copy link
Owner Author

LeoAndo commented Oct 25, 2022

rememberUpdatedState

値が変化しても再起動されない作用の値をキャプチャしたい場合に利用する。
具体的にはスプラッシュ画面などに利用される。

refs

LeoAndo/development-conference-memo#396
https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects?hl=ja#4
副作用の進行中に onTimeout が変更された場合、作用が終了したときに最後の onTimeout が呼び出されるという保証はありません。最後の onTimeout が呼び出されることを保証するには、rememberUpdatedState API を使用して onTimeout を記憶します。この API は、最新の値をキャプチャして更新します。

LeoAndo pushed a commit that referenced this issue Oct 26, 2022
LeoAndo pushed a commit that referenced this issue Oct 26, 2022
@LeoAndo
Copy link
Owner Author

LeoAndo commented Nov 8, 2022

DisposableEffect

キーが変化した後またはコンポーザブルが Composition から退場したときにクリーンアップする必要がある副作用については、DisposableEffect を使用します。
イベント登録/解除系はDisposableEffectを使うイメージ??

LeoAndo pushed a commit that referenced this issue Nov 8, 2022
LeoAndo pushed a commit that referenced this issue Nov 8, 2022
#69 DisposableEffectのサンプル
@LeoAndo
Copy link
Owner Author

LeoAndo commented Nov 8, 2022

SideEffect

Compose が管理していないオブジェクトと Compose の状態を共有するには、再コンポジションが成功するたびに呼び出される SideEffect コンポーザブルを使用します。

@LeoAndo
Copy link
Owner Author

LeoAndo commented Nov 8, 2022

produceState

produceState は、Composition をスコープとするコルーチンを起動します。これにより、返される State に値をプッシュできます。これを使用して、Compose 外の状態を Compose の状態に変換できます。

@LeoAndo
Copy link
Owner Author

LeoAndo commented Nov 9, 2022

derivedstateof

usecase:
https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects?hl=ja#8
のように、リストをスクロールして最初の要素が画面から消えた後、スクロール中に「最初へ移動」ボタンを常に表示させる

特定の状態が他の状態オブジェクトから計算(導出)される場合は、derivedStateOf を使用します。この関数を使用すると、計算で使用される状態のいずれかが変化したときにのみ計算が行われることが保証されます。

@LeoAndo
Copy link
Owner Author

LeoAndo commented Nov 9, 2022

snapshotFlow

Compose の State を Flow に変換するためのもの
State オブジェクトをコールド Flow に変換するには、snapshotFlow を使用します。

参考:
https://zenn.dev/kaleidot725/articles/2022-02-26-jc-snapshot-flow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant