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-create-state

v0.0.3

Published

useState (from React) but for Svelte.

Downloads

5

Readme

Svelte create state

useState (from React) but for Svelte.

Getting started

Install

npm i svelte-create-state

Usage

Basic

import { createState } from "svelte-create-state";

const [getCounter, setCounter] = createState(0);

setCounter(5)
getCounter() // 5

setCounter(n => n + 1)
getCounter() // 6

Subscribe changes

const [getStuff, setStuff, { subscribe }] = createState('chair');

subscribe(stuff => {
    console.log(stuff);
    // Log initial value: chair
    // Log second value: table
});

setStuff('table')

Svelte real example

<script>
  import { createState } from "svelte-create-state";

  const [getCount, setCount, { state: count }] = createState(0);

  function increment() {
    setCount(count => count + 1);
  }

  function decrement() {
    setCount(count => count - 1);
  }

  function logCurrentState() {
    console.log(getCount(), 'or', $count);
  }
</script>

<div>
  <div>
    <button on:click={increment}>+</button>
    {$count}
    <button on:click={decrement}>-</button>
  </div>
  <button on:click={logCurrentState}>Log state</button>
</div>

FAQ

What does calling createState do?

It declares a “state variable”. Our variable is called getCount but we could call it anything else, like getBanana. This is a way to “preserve” some values between the function calls — createState is a new way to use the exact same capabilities that writable provides in a svelte component.

What do we pass to createState as an argument?

The only argument to the createState() Hook is the initial state. The state doesn’t have to be an object. We can keep a number or a string if that’s all we need. In our example, we just want a number for how many times the user clicked, so pass 0 as initial state for our variable.

What does createState return?

It returns a pair of values: the current state and a function that updates it. This is why we write const [getCount, setCount] = createState(0). This is similar to writable(0), except you get them in a pair. If you’re not familiar with the syntax we used, read the next question.

What Do Square Brackets Mean?

You might have noticed the square brackets when we declare a state variable:

const [getCount, setCount] = createState(0);

The names on the left aren’t a part of the svelte-create-state or svelte. You can name your own state variables:

const [getFruit, setFruit] = createState('banana');

This JavaScript syntax is called “array destructuring”. It means that we’re making two new variables fruit and setFruit, where fruit is set to the first value returned by createState, and setFruit is the second. It is equivalent to this code:

var fruitStateVariable = createState('banana'); // Returns a pair
var getFruit = fruitStateVariable[0]; // First item in a pair
var setFruit = fruitStateVariable[1]; // Second item in a pair

When we declare a state variable with createState, it returns a pair — an array with two items. The first item is the current value, and the second is a function that lets us update it. Using [0] and [1] to access them is a bit confusing because they have a specific meaning. This is why we use array destructuring instead.

Contributing

Clone repository

To clone the repository use the following commands:

git clone https://github.com/juliandavidmr/svelte-create-state
cd svelte-create-state
npm install

Available Scripts

  • clean - remove coverage data, Jest cache and transpiled files,
  • build - transpile TypeScript to ES6,
  • build:watch - interactive watch mode to automatically transpile source files,
  • lint - lint source files and tests,
  • test - run tests,
  • test:watch - interactive watch mode to automatically re-run tests

Additional Information

Writing tests in JavaScript

Writing unit tests in TypeScript can sometimes be troublesome and confusing. Especially when mocking dependencies and using spies.

License

Licensed under the MIT. See the LICENSE file for details.