You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React provides the setState method which allows easily setting a subset, or all, of a component's state and triggering a render. Similarly, Few.swift provides the updateState function (but unfortunately does not make it easy to set just a piece of the state object). These functions, while simple, are valuable because they encourage a certain paradigm.
I personally use the following:
class Ref<T> {
var value: T
init(_ value: T) {
self.value = value
}
}
extension ComponentViewType {
func updateState<T: ComponentStateType>(@noescape block: (Ref<T?>) -> ()) {
let castedState = state as? T
let reference = Ref(castedState)
block(reference)
state = reference.value
renderComponent(CGSize.undefined)
}
}
So then updateState can then be called like so:
struct BookState: ComponentStateType {
var title: String
var author: String
}
// then in a button callback or something:
updateState{ (state: Ref<BookState?>) in
state.value!.title = "new title"
}
as opposed to currently needing to write:
var newState = myState!
newState.node = node
state = newState
renderComponent()
The (state: Ref<BookState?>) is necessary because the infra doesn't know what your ComponentStateType subclass is, but this can be solved if the state type is made a generic parameter of the view type, as Few.swift does.
If you think this seems reasonable I'd be happy to open a pull request! Interested to hear your thoughts.
The text was updated successfully, but these errors were encountered:
React provides the setState method which allows easily setting a subset, or all, of a component's state and triggering a render. Similarly, Few.swift provides the updateState function (but unfortunately does not make it easy to set just a piece of the state object). These functions, while simple, are valuable because they encourage a certain paradigm.
I personally use the following:
So then updateState can then be called like so:
as opposed to currently needing to write:
The
(state: Ref<BookState?>)
is necessary because the infra doesn't know what your ComponentStateType subclass is, but this can be solved if the state type is made a generic parameter of the view type, as Few.swift does.If you think this seems reasonable I'd be happy to open a pull request! Interested to hear your thoughts.
The text was updated successfully, but these errors were encountered: