Giter Site home page Giter Site logo

redux-connect-vue's Introduction

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:

redux-connect-vue's People

Contributors

kaidjohnson avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

wenjianmin

redux-connect-vue's Issues

Compatibility with vue/sort-keys linting rule

The Vue eslint team recently introduced a new "vue/sort-keys" rule - https://eslint.vuejs.org/rules/sort-keys.html

This rule can be used in place of eslint's sort-keys rule as it allows the component's top properties to remain in the style guide recommended/desired sort order (https://eslint.vuejs.org/rules/order-in-components.html) while allowing child property sort order to be enforced alphabetically. Previously vue/order-in-components and sort-keys where not compatible.

The trouble I'm having is getting the linter to recognize the default export where it's a function returning the object.

export default connect(state, actions)({
	components,
	data,
	computed,
	created,
	methods
});

In this example, the linter is applying sort-keys without regard to order-in-components and flagging data as needing to come after created.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency size-limit to v11
  • chore(deps): update redux to v5 (major) (redux, reselect)
  • chore(deps): update webpack (major) (babel-loader, webpack-cli)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @babel/core 7.17.9
  • @babel/preset-env 7.16.11
  • @size-limit/preset-small-lib 7.0.8
  • @vue/test-utils 2.0.0-rc.20
  • babel-core 7.0.0-bridge.0
  • babel-jest 27.5.1
  • babel-loader 8.2.5
  • eslint 8.13.0
  • eslint-plugin-jest 26.1.4
  • eslint-plugin-vue 8.6.0
  • jest 27.5.1
  • redux 4.2.0
  • reselect 4.1.5
  • size-limit 7.0.8
  • vue 3.2.33
  • webpack 5.72.0
  • webpack-cli 4.9.2
  • redux 4.x
  • vue 3.x
travis
.travis.yml
  • node 14

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.