-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: useReducer * test: useReducer * demo: useReducer
- Loading branch information
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/me/pavi2410/useCompose/app/screens/ReducerExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package me.pavi2410.useCompose.app.screens | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import me.pavi2410.useCompose.react.useReducer | ||
|
||
data class MyState(val count: Int) | ||
data class MyAction(val type: String) | ||
|
||
val initialState = MyState(0) | ||
|
||
@Composable | ||
fun ReducerExample() { | ||
Column { | ||
val (state, dispatch) = useReducer<MyState, MyAction>(initialState) { state, action -> | ||
when (action.type) { | ||
"increment" -> state.copy(count = state.count + 1) | ||
"decrement" -> state.copy(count = state.count - 1) | ||
else -> throw Error() | ||
} | ||
} | ||
|
||
Text("Count: ${state.count}") | ||
Button(onClick = { | ||
dispatch(MyAction("increment")) | ||
}) { | ||
Text("+") | ||
} | ||
Button(onClick = { | ||
dispatch(MyAction("decrement")) | ||
}) { | ||
Text("-") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters