npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-connect-vue

v3.0.0-rc.4

Published

A tiny Vue plugin that connects components with Redux

Downloads

295

Readme

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: