Giter Site home page Giter Site logo

revux's Introduction

revux

npm version Build Status Coverage Status

Inspired by Revue, use Redux with Vue.js seamlessly

We were not satisfied with the way the original Revue worked internally.

Basically, revux works by referencing your redux store on a Provider component. Every child component of the Provider will be able to access the store via a connect method: it aims to make the use of redux with vuejs a little more like react-redux

Installation

Install via NPM: npm i --save revux

API

connect connect([mapState], [mapDispatch])(Component)

mapState function is passed the state and the connected component's own props: mapState(state, [ownProps]) it should return a state object.

mapDispatch should be an object wrapping action creator functions - dispatch is bound automatically via redux's > bindActionCreators : mapDispatch = { foo: () => ({type: 'ACTION_CREATED'}) }

the result of mapState and mapDispatch will be accessible on your component's data properties

Getting started

For Vue-Router integration: check out the example folder

More details coming soon

store.js

Create your redux store:

import {createStore} from 'redux'
import reducer from './reducer'

const store = createStore(reducer)

export default store

index.js

import Vue from 'vue'
import revux from 'revux'
import main from './main.vue'

Vue.use(revux) // !!!

const app = new Vue({
  el: '#app',
  render: h => h(main)
})

main.vue

Use the Provider component from revux and bind your redux store. The store will be provided to all children components via vue's inject/provide mechanism.

<template>
    <Provider :store="store">
      <connectedComponent>
      </connectedComponent>
    </Provider>
</template>

<script>
  import store from './store'
  import connectedComponent from './connectedComponent.vue'

  export default {
    data () {
      return {
        store
      }
    },
    components: {
      connectedComponent
    }
  }
</script>

connectedComponent.vue

Import the connect method from revux to map state and actions to your instance's data. Here our state looks something like { status: 'foobar' }

<template>{{status}}</template>

<script>
  import { connect } from 'revux' // !!!
  import { createAction } from './actions'
  // createAction is an action creator of type () => ({type: 'ACTION_CREATED'})

  const component = {
    methods: {
      doMagic: function () {
        this.createAction()
      }
    }
  }

  const mapState = state => {
    const { status } = state
    return {
      status
    }
  })

  const mapDispatch = { createAction }

  export default connect(mapState, mapDispatch)(component)
</script>

NB:

You can also pass an object to mapState. Top-level object properties should be functions that get passed the state.

  const mapState = {
    status: state => state.status
  }

TODO:

  • Remove redux dependency
  • Clean up dev environement (rollup, poi, karma etc..)

revux's People

Contributors

edvincandon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

revux's Issues

Making the signature for mapDispatch more similar to the mapState signature

Hi there!

Thanks for this little library. I couldn't manage to make Revue or vue-redux work. I'm curious as to why you chose to expect different things for mapDispatch and mapState.

Would it be a good idea to also expec an object for mapState? I'd be more comfortable with something like this:

const mapState = {
  todos: state => state.todos.index,
  todo: state => state.todos.single,
}
const mapDispatch = {
  addTodo: todoActions.create,
}

connect(mapState, mapDispatch)(AwesomeComponent)

This is an issue mostly about aesthetics and consistence. The API works perfectly as-is.

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.