A tiny Vue plugin that connects components with Redux

A tiny Vue plugin that connects components with Redux

redux-connect-vue A tiny Vue plugin that connects components with Redux.No HOCs. No complex API. No dependencies. Just connect and go!

redux-connect-vue

A tiny Vue plugin that connects components with Redux.

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

  • Simple: < 60 lines code.
  • Tiny: < 0.3 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.

connect(mapStateToProps, mapDispatchToProps)

  • mapStateToProps: Argument that gets passed to mapStateToPropsFactory. Binds to component $data (available on the vm directly).
  • mapDispatchToProps: Argument that gets passed to mapDispatchToPropsFactory. Binds to component $actions (available at vm.$actions).
  • returns a function that receives the Vue component configuration.

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 ReduxConnectVue from 'redux-connect-vue';
import store from './store.js';

Vue.use(ReduxConnectVue, { store });

Connect your state and actions to your component:

// component.vue

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

const state = (state) => ({
  foo: state.foo
});

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

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

Example using bindActionCreators as mapDispatchToPropsFactory

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

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

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

const state = (state) => ({
  foo: state.foo
});

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

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

Example using createStructuredSelector from Reselect as mapStateToPropsFactory

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

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

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

const state = {
  foo: createSelector((state) => state.foo, (foo) => foo.toUpperCase())
};

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

export default connect(state, actions)({
  template: '<p>Foo: {{ foo }}</p><button @click="$actions.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:

Download Details:

Author: kaidjohnson

Source Code: https://github.com/kaidjohnson/redux-connect-vue

vue vuejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

8 Popular Websites That Use The Vue.JS Framework

In this article, we are going to list out the most popular websites using Vue JS as their frontend framework. Vue JS is one of those elite progressive JavaScript frameworks that has huge demand in the web development industry. Many popular websites are developed using Vue in their frontend development because of its imperative features.

Vue Native is a framework to build cross platform native mobile apps using JavaScript

Vue Native is a framework to build cross platform native mobile apps using JavaScript. It is a wrapper around the APIs of React Native. So, with Vue Native, you can do everything that you can do with React Native. With Vue Native, you get

How to Make a Simple Vue Custom Select Component

In this article, you’ll learn how to build a Vue custom select component that can be easily be styled using your own CSS. In fact, it’s the same component that we use in production on Qvault, and you can see it in action on the playground.

Creating a Custom Tooltip Component in Vue

There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time.

Vue ShortKey plugin for Vue.js

Vue-ShortKey - The ultimate shortcut plugin to improve the UX .Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.