Skip to content

Instantly share code, notes, and snippets.

@mrampazz
Created September 30, 2020 09:31
Show Gist options
  • Save mrampazz/4185152884cf268de85d01bea1ef5ea1 to your computer and use it in GitHub Desktop.
Save mrampazz/4185152884cf268de85d01bea1ef5ea1 to your computer and use it in GitHub Desktop.
Redux-react implementation kotlin
import actions.ToggleSideBar
import react.*
import react.redux.rConnect
import reducers.AppState
import redux.WrapperAction
private interface TableStateProps : RProps {
var isSideBarOpen: Boolean
}
private interface TableDispatchProps : RProps {
var toggleSideBar: (Boolean) -> Unit
}
val wrappedTable: RClass<RProps> =
rConnect<AppState, ToggleSideBar, WrapperAction, RProps, TableStateProps, TableDispatchProps, TableProps>(
mapStateToProps = { state, _ ->
isSideBarOpen = state.isSideBarOpen
},
mapDispatchToProps = { dispatch, _ ->
toggleSideBar = { dispatch(ToggleSideBar(it)) }
}
)(TableComponent::class.js.unsafeCast<RClass<TableProps>>())
package components
import kotlinx.html.js.onClickFunction
import react.RBuilder
import react.RComponent
import react.RProps
import react.RState
import react.dom.button
import react.dom.div
external interface TableProps : RProps {
var isSideBarOpen: Boolean
var toggleSideBar: (Boolean) -> Unit
}
class TableComponent(props: TableProps) : RComponent<TableProps, RState>(props) {
override fun RBuilder.render() {
div {
if (props.isSideBarOpen) {
+"is open"
} else {
+"is closed"
}
button {
attrs.onClickFunction = { props.toggleSideBar(!props.isSideBarOpen) }
+"click me"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment