3.0.0-rc.4 • Published 2 years ago

redux-connect-vue v3.0.0-rc.4

Weekly downloads
42
License
MIT
Repository
github
Last release
2 years ago

npm Depfu Build Status Test Coverage Maintainability GitHub

redux-connect-vue

**Note: redux-connect-vue v3 is compatible with Vue 3.x. If you are looking for a Vue 2.x compatible version, check out v2

A tiny Vue plugin that connects components with Redux.

No HOCs. No complex API. No dependencies. Just use and go!

  • Simple: < 60 lines code.
  • Tiny: < 0.4 KB gzipped.
  • Flexible: Configurable api via Vue plugin options.

New to Redux? Start Here

Installation

npm install redux-connect-vue

API

Vue Plugin Options

  • store (required): Redux store (or any other store object that has getState, dispatch, and subscribe methods)
  • mapDispatchToStateFactory (optional): Factory function that receives the supplied mapStateToProps and returns a function that receives state and returns an {Object} of props. Defaults to an identity function.
  • mapDispatchToPropsFactory (optional): Factory function that receives the supplied mapDispatchToProps and returns a function that receives dispatch and returns an {Object} of actions. Defaults to an identity function.

useState(mapStateToProps)

  • mapStateToProps: Argument that gets passed to mapStateToPropsFactory.
  • returns an object to be included in a setup() return

useActions(mapDispatchToProps)

  • mapDispatchToProps: Argument that gets passed to mapDispatchToPropsFactory.
  • returns an object to be included in a setup() return

Standard Example

Set up a Redux store:

// store.js

import { createStore, combineReducers } from 'redux';
import fooReducer from './foo/reducer.js';

export default createStore(combineReducers({
  foo: fooReducer
}));

Install the redux-connect-vue plugin:

import { createApp } from 'vue';
import ReduxConnectVue from 'redux-connect-vue';
import store from './store.js';

createApp(...).use(ReduxConnectVue, { store });

Connect your state and actions to your component:

// component.vue

<script>
import { useState } from 'redux-connect-vue';

export default {
  setup() {
    const state = useState((state) => ({
      foo: state.foo
    }));

    const actions = useActions((dispatch) => ({
      doSomething: (payload) => dispatch({ type: 'DO_SOMETHING', paylaod })
    }));

    return {
      ...state,
      ...actions
    };
  },
  template: '<p>Foo: {{ foo }}</p><button @click="doSomething('hello')"></button>'
};
</script>

Example using bindActionCreators as mapDispatchToPropsFactory

import { bindActionCreators } from 'redux';
import { createApp } from 'vue';
import ReduxConnectVue from 'redux-connect-vue';
import store from './store.js';

createApp(...).use(ReduxConnectVue, {
  store,
  mapDispatchToPropsFactory: (actionCreators) => (dispatch) => bindActionCreators(actionCreators, dispatch)
});
// component.vue

<script>
import { useActions, useState } from 'redux-connect-vue';

export default {
  setup() {
    const state = useState((state) => ({
      foo: state.foo
    }));

    const actions = useActions({
      doSomething: (payload) => ({ type: 'DO_SOMETHING', payload })
    });

    return {
      ...state,
      ...actions
    };
  },
  template: '<p>Foo: {{ foo }}</p><button @click="doSomething('hello')"></button>'
};
</script>

Example using createStructuredSelector from Reselect as mapStateToPropsFactory

import { bindActionCreators } from 'redux';
import { createApp } from 'vue';
import { createStructuredSelector } from 'reselect';
import ReduxConnectVue from 'redux-connect-vue';
import store from './store.js';

createApp(...).use(ReduxConnectVue, {
  store,
  mapDispatchToPropsFactory: (actionCreators) => (dispatch) => bindActionCreators(actionCreators, dispatch),
  mapStateToPropsFactory: createStructuredSelector
});
// component.vue

<script>
import { useActions, useState } from 'redux-connect-vue';
import { createSelector } from 'reselect';

export default {
  setup() {
    const state = useState({
      foo: createSelector((state) => state.foo, (foo) => foo.toUpperCase())
    });

    const actions = useActions({
      doSomething: (payload) => ({ type: 'DO_SOMETHING', payload })
    });

    return {
      ...state,
      ...actions
    };
  },
  template: '<p>Foo: {{ foo }}</p><button @click="doSomething('hello')"></button>'
};
</script>

Yet another Redux library for Vue. Why!?

The source code for Redux is approachable. A Redux connection library should be just as approachable so you can start building things with minimal overhead.

This library is opinionated, as is each of the other libraries out there. It was built with the following best practices in mind:

There are a lot of options out there that each do similar things, in case you're looking for something else:

3.0.0-rc.2

2 years ago

3.0.0-rc.1

2 years ago

3.0.0-rc.4

2 years ago

3.0.0-rc.3

2 years ago

1.1.3

3 years ago

1.1.2

3 years ago

2.0.0-beta.1

3 years ago

1.1.1

3 years ago

1.1.0

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago