0.0.53 • Published 7 months ago

vue-connector v0.0.53

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

vue-connector

🪡 Connect anything to your vue components.

The idea for this library comes from an article about Presentational and Container Components. If you are wondering whether to use it, I would recommend this article to you.

Installation

pnpm install vue-connector

Usage

Our purpose is straightforward, connect the state to the Presentational Component, or intercept and preprocess the props of the Presentational Component.

vue-connector has already intercepted props for you. The important thing is that you need to tell it how you get the state, combine it into props, and pass it to the Presentational Component.

import { defineConnector } from 'vue-connector'

/**
 * mapStateProps allows you to define reactive props
 * that it will be executed every time props and reactive dependencies inside mapStateProps change
 *
 * @param ownProps - component props
 * @param instance - the current component instance
 */
const mapStateProps = (ownProps, instance) => {
  return {
    // return state props
  }
}

/**
 * since mapStateProps is reactive, you can also define a factory function,
 * which allows you to define some closure variables before mapStateProps is executed
 *
 * Note: mapStatePropsFactory will only be executed once during `setup`
 *
 * @param initialProps - the first props
 * @param instance - the current component instance
 */
const mapStatePropsFactory = (initialProps, instance) => {
  return (ownProps, instance) => {
    return {
      // return state props
    }
  }
}

/**
 * mapStaticProps allows you to define static props
 * that it will only be executed once during `setup`.
 *
 * @param ownProps - component props
 * @param instance - the current component instance
 */
const mapStaticProps = (ownProps, instance) => {
  return {
    // return static props
  }
}

/**
 * mergeProps allows you to customize the merging logic of props,
 * it is eventually passed to the connected component
 *
 * The default is `{ ...ownProps, ...stateProps, ...staticProps }`.
 *
 * @param stateProps - props from mapStateProps
 * @param staticProps - props from mapStaticProps
 * @param ownProps - component props
 */
const mergeProps = (stateProps, staticProps, ownProps) => {
  return {
    // merged props
  }
}

/**
 * Define your connector, which defines how you get the state
 *
 * @param {?mapStateProps|mapStatePropsFactory} mapStateProps
 * @param {?mapStatePropsFactory} mapStatePropsFactory
 * @param {?mergeProps} mergeProps
 */
const connector = defineConnector(mapStateProps, mapStaticProps, mergeProps)

const ConnectComponent = connector(PresentationComponent)

The playground contains usage in conjunction with some popular state libraries.

License

MIT

0.0.40

11 months ago

0.0.41

11 months ago

0.0.42

11 months ago

0.0.43

9 months ago

0.0.44

9 months ago

0.0.45

9 months ago

0.0.46

9 months ago

0.0.47

9 months ago

0.0.39

11 months ago

0.0.51

7 months ago

0.0.52

7 months ago

0.0.53

7 months ago

0.0.50

8 months ago

0.0.48

8 months ago

0.0.49

8 months ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago