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

svelte-redux

v1.1.1

Published

Connect svelte components to a redux store.

Downloads

14

Readme

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>