• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Single-function alternative for React.useState

use-st8

Single-function alternative for React.useState

npm size Donate

Installation

npm add use-st8

Quick example

usest8 is a single function alternative for the useState hook (typically: const [currentValue, updater] = useState(initial)), that combines the current value constant and updater function into a single function.

import * as React from "react";
import { render } from "react-dom";
import { useSt8 } from "use-st8"; // (or) import useSt8 from 'use-st8';

function App() {
  const count = useSt8(0);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>{count()}</h2>
      <button onClick={() => count(c => c - 1)}>-</button>
      <button onClick={() => count(c => c + 1)}>+</button>
      <button onClick={() => count(0)}>Reset</button>
    </div>
  );
}

const rootElement = document.getElementById("root");
render(<App />, rootElement);

Open the demo in code-sandbox to see it in action

API

// create a new local state value in a React function component
const count = useSt8(0)

// same, but with initializer function
const count = useSt8(() => 0)

// get the current state
count() 

// change the state with to the given value
count(0)

// update the state using an updater function, that receives the current state and returns the next one
count(c => c + 1)

useSt8 has the same call-signature as the React useState hook. Except, instead of returning a tuple with the current value and a setter, it returns a single function. The function returned can be called in two different ways:

  • With zero arguments. In that case the current state is returned.
  • With one argument. In that case the current state is updated with the given value, or using an updater function, just like the normal useState update function.

That's all.

Benefits

  • No array destructurings needed, which polute your closures with name pairs, like const [count, setCount] = useState(0). Instead, const count = useSt8(0) just reads nicer. And it saves some characters. Super important. All IMHO 😉.
  • 🚀 Doesn't rely on array destructurings, which are potentially slow as they use the iterator protocol (background). Note that you probably won't notice this in practice, so this is more of a fun fact than an argument to use this.

Example

With useState (offical example):

import { useState } from "react"

function Counter({initialCount}) {
  const [count, setCount] = useState(initialCount);
  return (
    <>
      Count: {count}
      <button onClick={() => setCount(0)}>Reset</button>
      <button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
      <button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
    </>
  );
}

With useSt8:

import { useSt8 } from "use-st8"

function Counter({initialCount}) {
  const count = useSt8(initialCount);
  return (
    <>
      Count: {count()}
      <button onClick={() => count(0)}>Reset</button>
      <button onClick={() => count(prevCount => prevCount + 1)}>+</button>
      <button onClick={() => count(prevCount => prevCount - 1)}>-</button>
    </>
  );
}

[sarcasm]Which saves a whooping 21 characters. Now go forth and refactoring all the things![/sarcasm]

The name

useSt8 is a shorter form of useState, which has 8 characters. Also, the pronounciation is pretty similar to "useState".

If you prefer to use with a different name, the useSt8 named export is set as the default export as well.

import useSt8 from 'use-st8';
import useCustomNameSt8 from 'use-st8';

Cool?

Do you think this cool and useful? Consider buying me a coffee!
Buy Me A Coffee

More Repositories

1

relative-deps

Installs local dependencies for optimal developer experience
JavaScript
428
star
2

import-size

Measure the real, minified, tree-shaken impact of individual imports into your app
JavaScript
123
star
3

redux-todomvc

Redux todoMVC, used to do some benchmarking
JavaScript
106
star
4

state-routing-blog-sources

JavaScript
88
star
5

remmi

Nothing to see here. Go away
TypeScript
86
star
6

rval

minimalistic transparent reactive programming library
TypeScript
85
star
7

react-mobx-shop

71
star
8

mst-course

Lesson sources for "Manage Application State with Mobx-state-tree" course
JavaScript
62
star
9

nscript

Write shell scripts like a bearded guru by unleashing your javascript skills!
JavaScript
60
star
10

mobx-todomvc

Trimmed down TodoMVC used for benchmarking
JavaScript
45
star
11

ts-npm-lint

Utility to help you to create typescript based npm modules. Can also strip reference paths.
JavaScript
38
star
12

willing-people

People you might want to ask instead of me
19
star
13

immer-gifts

Example accompanying the Immer course on egghead
JavaScript
19
star
14

react-next-2017-demo

JavaScript
10
star
15

miniup

Light-weight easy-to-use PEG parser with some toppings!
TypeScript
8
star
16

flipper-data-source-demo

TypeScript
6
star
17

NOA

A reflective data and calculating model for javascript
TypeScript
4
star
18

reactive2016-slides

The Quest For Immer Mutable State Management
HTML
4
star
19

math-tester

For my kids
TypeScript
4
star
20

mxgit

[unmaintained] Small tool that helps versioning Mendix projects with git.
JavaScript
4
star
21

react-ts-conversion-exercise

JavaScript
4
star
22

magix-mobx-slides

Slides from RuhrJS 2016 "Magic MobX" presentation
JavaScript
2
star
23

currencies-demo

JavaScript
2
star
24

clutility

The minimalistic class and inheritance utility for javascript.
JavaScript
2
star
25

flowmachine

TypeScript
1
star
26

reactjsday2017-presentation

HTML
1
star
27

react-next-2017-slides

HTML
1
star
28

reatjsday2017-demo

JavaScript
1
star