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

react-vuex

v0.4.1

Published

React bindings for Vuex

Downloads

101

Readme

react-vuex

React bindings for Vuex inspired by react-redux project.

example workflow example workflow Codecov Status Coveralls Status

Donate with Liberapay

Installation

react-vuex requires React 16.0+, Vue 2.0+ and Vuex 3.0+

npm install --save react-vuex

This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.

Documentation

To look at some example projects, take a look at the examples section of this repo.

Example use

  • configure your store with state, getters, mutations and actions, according to Vuex documentation

    /*
     * actions.js
     */
    
    export const INCREMENT_ASYNC = 'INCREMENT_ASYNC';
    
    export default {
      incrementAsync: (value = 1) => ({
        type: INCREMENT_ASYNC,
        value,
      }),
    };
    
    
    /*
     * mutations.js
     */
    
    export const INCREMENT = 'INCREMENT';
    export const INCREMENT_START = 'INCREMENT_START';
    export const INCREMENT_STOP = 'INCREMENT_STOP';
    
    export default {
      increment: (value = 1) => ({
        type: INCREMENT,
        value,
      }),
    };
    
    
    /*
     * store.js
     */
    
    import Vue from 'vue';
    import Vuex from 'vuex';
    import { INCREMENT, INCREMENT_START, INCREMENT_STOP } from './mutations';
    import { INCREMENT_ASYNC } from './actions';
    
    Vue.use(Vuex);
    
    export default new Vuex.Store({
      state: {
        count: 0,
        isIncrementing: false,
      },
      getters: {
        countGreaterThan2: (state, getters) => state.count > 2,
      },
      mutations: {
        [INCREMENT](state) {
          state.count += 1;
        },
        [INCREMENT_START](state) {
          state.isIncrementing = true;
        },
        [INCREMENT_STOP](state) {
          state.isIncrementing = false;
        },
      },
      actions: {
        [INCREMENT_ASYNC]({ commit, state }, payload) {
          commit(INCREMENT_START);
          return new Promise((resolve) => {
            setTimeout(() => {
              commit(INCREMENT);
              resolve();
            }, 500);
          }).then(() => commit(INCREMENT_STOP))
            .then(() => state.count);
        },
      },
    });
  • use Provider in your app

    This will pass the store to all subcomponents of the app

    /*
     * index.js
     */
    
    import React from 'react';
    import { render } from 'react-dom';
    import { Provider } from 'react-vuex';
    import App from './components/App';
    import store from './store';
    
    render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root'),
    );
  • create your container component mapping state, dispatch, commit and getter to the store

    /*
     * containers/MyContainer.js
     */
    
    import { connect } from 'react-vuex';
    import MyComponent from '../components/MyComponent';
    import mutations from '../mutations';
    import actions from '../actions';
    
    const mapStateToProps = (state, ownProps) => ({
      myCount: state.count,
    });
    const mapDispatchToProps = (dispatch, ownProps) => ({
      onIncrementAsync: val => dispatch(actions.incrementAsync(val)),
    });
    const mapCommitToProps = (commit, ownProps) => ({
      onIncrement: () => commit(mutations.increment()),
    });
    const mapGetterToProps = (getter, ownProps) => ({
      isGreaterThan2: getter.countGreaterThan2,
    });
    
    const MyContainer = connect(
      mapStateToProps,
      mapDispatchToProps,
      mapCommitToProps,
      mapGetterToProps,
    )(MyComponent);
    
    export default MyContainer;
  • create your presentational component using mapped props

    /*
     * components/MyComponent.js
     */
    
    import React from 'react';
    import PropTypes from 'prop-types';
    
    export default class MyComponent extends React.PureComponent {
      constructor(props, context) {
        super(props, context);
        this.handleInc = this.handleInc.bind(this);
        this.handleIncAsync = this.handleIncAsync.bind(this);
      }
      handleInc() {
        if (this.props.onIncrement) {
          this.props.onIncrement();
        }
      }
      handleIncAsync() {
        if (this.props.onIncrementAsync) {
          this.props.onIncrementAsync().then(() => {}));
        }
      }
      render() {
        return (
          <div>
            Count is {this.props.myCount !== undefined && `${this.props.myCount}, `}
            greater than 2: {this.props.isGreaterThan2 ? 'yes' : 'no'}
            {this.props.onIncrement &&
              <button onClick={this.handleInc}>Increment Sync</button>
            }
            {this.props.onIncrementAsync &&
              <button onClick={this.handleIncAsync}>Increment Async</button>
            }
          </div>
        );
      }
    }
    
    MyComponent.defaultProps = {
      children: undefined,
      isGreaterThan2: false,
      myCount: 0,
      onIncrement: undefined,
      onIncrementAsync: undefined,
    };
    
    MyComponent.propTypes = {
      children: PropTypes.node,
      isGreaterThan2: PropTypes.bool,
      myCount: PropTypes.number,
      onIncrement: PropTypes.func,
      onIncrementAsync: PropTypes.func,
    };
  • use your container component in app

    import React from 'react';
    import MyContainer from '../containers/MyContainer';
    
    export default class App extends React.Component {
      constructor(props, context) {
        super(props, context);
        this.state = {
          testValue: 123,
        };
      }
    
      render() {
        return (
          <div>
            <MyContainer />
          </div>
        );
      }
    }

License

MIT

Support on Beerpay or Liberapay

Hey dude! Help me out for a couple of :beers:!

Beerpay Beerpay

Donate with Liberapay