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-adapter

v1.0.4

Published

A simple wrapper on `Redux` and makes it possible to use `Redux` store on svelte.

Downloads

6

Readme

Svelte Redux Adapter

A simple wrapper on Redux and makes it possible to use Redux store on svelte.

Under the hook, it adapts Redux store's subscribe method to svelte's store contract, without any extra Readable, Derived overhead.

npm version

Demo Counter App

Install

Install svelte-redux-adapter package

npm install svelte-redux-store

Install redux package

npm install @reduxjs/toolkit

API

Provider

A svelte component that injects store instance to context.

Example

  • Define store
        // src/counter-store.ts
    import {configureStore, createSlice} from '@reduxjs/toolkit'
    const counter = createSlice({
      name: 'counter',
      initialState: {
        count: 0
      },
      initialState: { count: 0 },
      reducers: {
        increment: state => {
          state.count++
        },
        decrement: state => {
          state.count--
        },
        reset: _ => ({
          count: 0,
        }),
      },
    })
    const reduxStore = configureStore({
      reducer: {
        counter: counter.reducer,
      },
    }) 
  • provide store to context
      <script lang="ts">
      import Provider from '$lib/Provider.svelte'
      import { reduxStore } from './counter-store'
      </script>
    
      <div class="app">
        <main>
          <Provider store={reduxStore}>
            <slot />
          </Provider>
        </main>
      </div>

useSelector()

Creates a store similar to Derived from part of your store using selector function. The result of selector is memoized and compared by ===

useStore(), useDispatch()

Hooks to grab store, dispatch from nearest Provider

Example

<script lang="ts">
  import { spring } from 'svelte/motion'
  import { useSelector, useStore } from 'svelte-redux-adapter'
  import { type RootState, actions } from './counter-store'

  const store = useStore<RootState>()

  /** Derive your state using either selector */
  const count = useSelector((rootState: RootState) => rootState.counter.count)
  /** Or using reactive statement */
  $: count3x = $store.counter.count * 3

  const displayed_count = spring()
  $: displayed_count.set($count)
  $: offset = modulo($displayed_count, 1)

  const displayed_count_3x = spring()
  $: displayed_count_3x.set(count3x)
  $: offset_3x = modulo($displayed_count_3x, 1)

  function modulo(n: number, m: number) {
    // handle negative numbers
    return ((n % m) + m) % m
  }
</script>

<div class="counter">
  <button
    on:click={() => store.dispatch(actions.decrement())}
    aria-label="Decrease the counter by one"
    data-testid="decrement_btn">
    <svg aria-hidden="true" viewBox="0 0 1 1">
      <path d="M0,0.5 L1,0.5" />
    </svg>
  </button>

  <div class="counter-viewport">
    <div class="counter-digits" style="transform: translate(0, {100 * offset}%)">
      <strong class="hidden" aria-hidden="true">{Math.floor($displayed_count + 1)}</strong>
      <strong data-testid="display_count">{Math.floor($displayed_count)}</strong>
    </div>
  </div>

  <div class="counter-viewport">
    <div class="counter-digits" style="transform: translate(0, {100 * offset_3x}%)">
      <strong class="hidden" aria-hidden="true">{Math.floor($displayed_count_3x + 1)}</strong>
      <strong data-testid="display_count_3x">{Math.floor($displayed_count_3x)}</strong>
    </div>
  </div>

  <button
    on:click={() => store.dispatch(actions.increment())}
    aria-label="Increase the counter by one"
    data-testid="increment_btn">
    <svg aria-hidden="true" viewBox="0 0 1 1">
      <path d="M0,0.5 L1,0.5 M0.5,0 L0.5,1" />
    </svg>
  </button>
</div>

store.value

Those adapted stores, either from redux or useSelector, have an extra getter .value. It helps you get memoized value from store instantly which is faster and cleaner than using built-in store/get.

const count = useSelector((rootState: RootState) => rootState.counter.count)

console.log(count.value === get(count)) // print: true

Contributing ✨

Interested in contributing to this repo? Check out our and submit a PR for a new feature/bug fix.

A big shoutout to all our contributors! You could be here too! ✨

Inspire by

svelte-redux-store