Classes
The following classes are available globally.
-
A ReRxSwift Connection that manages the mapping between ReSwift application state and ReSwift actions on the one hand, and view controller props and actions on the other hand.
In order to use this, you have to make your view controller conform to
Connectable
and add aConnection
instance. In your view controller you need to callconnect()
anddisconnect()
at the right time.Examples
The folder
Example
contains example of how to use this.Usage
Create an extension to your view controller to make it
Connectable
, defining theProps
andActions
that your view controller needs:extension MyViewController: Connectable { struct Props { let text: String } struct Actions { let updatedText: (String) -> Void } }
Define how your state is mapped to the above
Props
type:private let mapStateToProps = { (appState: AppState) in return MyViewController.Props( text: appState.content ) }
Define the actions that are dispatched:
private let mapDispatchToActions = { (dispatch: @escaping DispatchFunction) in return MyViewController.Actions( updatedText: { newText in dispatch(SetContent(newContent: newText)) } ) }
Define the connection and hook it up:
class MyViewController: UIViewController { @IBOutlet weak var textField: UITextField! let connection = Connection( store: store, mapStateToProps: mapStateToProps, mapDispatchToActions: mapDispatchToActions ) override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) connection.connect() } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) connection.disconnect() } }
Bind the text field’s text, using a Swift 4 key path to refer to the
text
property ofProps
:override func viewDidLoad() { super.viewDidLoad() connection.bind(\Props.text, to: textField.rx.text) }
Call the action:
@IBAction func editingChanged(_ sender: UITextField) { actions.updatedText(sender.text ?? "") }
Declaration
Swift
public class Connection<State, Props, Actions> : StoreSubscriber where State : StateType