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 a Connection instance. In your view controller you need to call connect() and disconnect() at the right time.

    Examples

    The folder Example contains example of how to use this.

    Usage

    1. Create an extension to your view controller to make it Connectable, defining the Props and Actions that your view controller needs:

      extension MyViewController: Connectable {
          struct Props {
              let text: String
          }
          struct Actions {
              let updatedText: (String) -> Void
          }
      }
      
    2. Define how your state is mapped to the above Props type:

      private let mapStateToProps = { (appState: AppState) in
          return MyViewController.Props(
              text: appState.content
          )
      }
      
    3. Define the actions that are dispatched:

      private let mapDispatchToActions = { (dispatch: @escaping DispatchFunction) in
          return MyViewController.Actions(
              updatedText: { newText in dispatch(SetContent(newContent: newText)) }
          )
      }
      
    4. 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()
          }
      }
      
    5. Bind the text field’s text, using a Swift 4 key path to refer to the text property of Props:

      override func viewDidLoad() {
          super.viewDidLoad()
          connection.bind(\Props.text, to: textField.rx.text)
      }
      
    6. Call the action:

      @IBAction func editingChanged(_ sender: UITextField) {
          actions.updatedText(sender.text ?? "")
      }
      
    See more

    Declaration

    Swift

    public class Connection<State, Props, Actions> : StoreSubscriber where State : StateType