Skip to content

defconcepts/revue

Repository files navigation

Revue

NPM version NPM download Build Status

Usage

New: Time to use Redux-devtools in non-React apps!

Obviously it works with Redux, install via NPM: npm i -D redux revue

You can also hot-link the CDN version: https://npmcdn.com/revue/revue.js, Revue is exposed to window object.

// App.js
import Revue from 'revue'
import store from './store'
import actions from './actions'
Vue.use(Revue, {
  store,
  // if you want to call your actions from vm instance
  // then your vm you can call like `this.$actions.addTodo(todo)`
  actions
})

// store.js
// just put some reducers in `./reducers` like
// what you do in pure Redux
// and combine them in `./reducers/index.js`
import { createStore } from 'redux'
import reducer from './reducers/index'
export default createStore(reducer)

// component.js
// some component using Revue
new Vue({
  el: '#app',
  data () {
    return {
      counter: this.$store.state.counter
    }
  },
  ready () {
    // subscribe state changes
    this.$subscribe('counter')
    // if your name the 'counter' to 'temp_counter' in data()
    // you can use this.$subscribe('counter as temp_counter')
    // if you want to subscribe a deep property
    // this.$subscribe('top.middle.counter as counter')
    // or even this.$subscribe('something.in.reduxStore.counter as instance.somewhere.counter')
    // you can only $subscribe once, if you want to subscribe multi states at the same time, do this:
    /*
    this.$subscribe(
      'foo',
      'bar'
    )
    */
  },
  methods: {
    handleClickCounter () {
      // dispatch events
      this.$store.dispatch({type: 'INCREMENT'})
    }
  }
})

More detailed usages

Hot-reload reducers

Just change your store.js like this:

Before:

import { createStore } from 'redux'
import rootReducer from './reducers'

export default createStore(rootReducer)

After:

import { createStore } from 'redux'
import rootReducer from './reducers'

function configureStore() {
  const store = createStore(rootReducer)
  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const nextRootReducer = require('./reducers').default
      store.replaceReducer(nextRootReducer)
    })
  }
  return store
}

export default configureStore()

FAQ

Do I have to use this.$subscribe? It's so verbose.

No, not always if you don't care about mutating states in reducers. And also because Vue states are mutable and observable, it's ok for you to modify data directly like state.foo = 'bar', then it becomes so similar to the Vue Flux implementation Vuex, it allows you to mutate data. However what the best part of Redux is states are immutable, which means you can't make direct operations on states so that you have less chance to make mistakes.

this.$subscribe is only needed if you don't mutate states directly. And you're recommended to do so.

License

MIT © EGOIST