Code Monkey home page Code Monkey logo

svelte-redux's Introduction

Svelte Redux

Connect svelte components to a redux store.

Install

npm i svelte-redux

Usage

Connect the store

Assuming you already have a store set up, you need to connect it to your svelte component. You do this like so:

export default {
    store: connect(store, mapStateToData, mapDispatchToStore),

    // ...
};

If you've used redux with react, this should be quite familiar.

Map the state

The mapStateToData property is a function that, given the current state, returns the data you want to consume in this component.

In this very simplistic example, our whole state is just a number (we're building a simple counter) so you might map it as below:

const mapStateToData = state => {
    return {
        count: state,
    };
};

Using the data

Now that we've mapped our state to count, we can access it in the template like you would with local state, however it is prepended with a $.

<h1>
    Clicked {{$count}} times
</h1>

Dispatching actions

In order to dispatch actions, you must first map a dispatch function to the store.

const mapDispatchToStore = dispatch => {
    return {
        increment: () => dispatch({ type: 'INCREMENT' }),
        decrement: () => dispatch({ type: 'DECREMENT' }),
    };
};

And now we can access these functions in the template via the store

<button on:click="store.increment()">+</button>
<button on:click="store.decrement()">-</button>

Optimisations

At this point everything should be working, there are however a few things we can change for convenience.

The mapDispatchToStore function is quite verbose given the simple goal so we can instead use the shorthand:

const mapDispatchToStore = {
    increment: () => ({ type: 'INCREMENT' }),
    decrement: () => ({ type: 'DECREMENT' }),
};

Passing store to connect every time is slightly inconvenient and requires all your components to know about store. We can use the bindConnect to make this slightly nicer.

const connect = bindConnect(store);

// ...

export default {
    store: connect(mapStateToData, mapDispatchToStore),
};

And now we can use this connect function instead of the one imported from svelte-redux

Full Example

store.js
import { createStore } from 'redux';
import { bindConnect } from 'svelte-redux';

const store = createStore((state = 0, action) => {
    switch (action.type) {
        case 'INCREMENT':
            return state + 1;
        case 'DECREMENT':
            return state - 1;
        default:
            return state;
    }
});

export const connect = bindConnect(store);
Counter.html
<h1>
    Clicked {{$count}} times
</h1>
<button on:click="store.increment()">+</button>
<button on:click="store.decrement()">-</button>

<script>
    import { connect } from './store';

    const mapStateToData = state => {
        return {
            count: state,
        };
    };

    const mapDispatchToStore = {
        increment: () => ({ type: 'INCREMENT' }),
        decrement: () => ({ type: 'DECREMENT' }),
    }

    export default {
        store: connect(mapStateToData, mapDispatchToStore)
    }
</script>

svelte-redux's People

Contributors

jamesbirtles avatar

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.