1.2.8 • Published 1 year ago

statelake v1.2.8

Weekly downloads
9
License
ISC
Repository
-
Last release
1 year ago

StateLake

A StateLake is a state container for React, built on top of the React hooks api.

It's been designed to seamlessly replace normal React hooks and states wherever you need to share state among multiple components.

// Normal React hooks
const [state, setState] = React.useState();

// StateLake hooks
const [state, setState] = store.useState();

Running on top of hooks, StateLakes are lightweight, easy to use, and integrates really well with TypeScript and IntelliSense.

By using TypeScript you'll also get type-safe states in every step of the process.

Note: This project is still being tested, and may still have some bugs. If you find a problem, please create a new issue, and feel free to contribute. Thanks!

npm-version downloads

Getting started

Prerequisites

You'll need React v16.8 or higher. This library utilizes React hooks, but does not install this dependency on it's own.

Installation

npm install statelake

Usage

import { StateLake } from 'statelake';

/**
 * Recommended, not necessary:
 * Start by creating an interface to describe the shape of your store.
 *
 * This will enable IntelliSense and type checking to work with your entire store,
 * so you don't have to input your types anywhere else.
 */
interface IStore {
  show_sidebar: boolean;
  current_note: string;
  notes: {
    [note_id: string]: {
      title: string;
      body: string;
      timestamp: number;
    };
  };
}

/**
 * Initialize the store.
 * By providing the store interface this way you'll get IntelliSense
 * working with you all the way.
 */
const store = new StateLake<IStore>({
  show_sidebar: false,
  current_note: '',
  notes: {}
});

/**
 * Now let's create a buton and connect it to the "show_sidebar"
 * property in our store.
 */
function ShowSidebarButton() {
  // Lake states
  const [show, setShow] = store.useState('show_sidebar');

  // Toggle show/hide sidebar
  const toggleSidebar = () => setShow(!show);

  // Render
  return (
    <button onClick={toggleSidebar}>
      {show ? 'Hide sidebar' : 'Show sidebar'}
    </button>
  );
}

/**
 * Render a note that the user can edit
 */
function Note({ id }: { id: string }) {
  // Reference the branch this node will use
  const branch = store.useBranch('notes', id);

  // Lake states
  const [title, setTitle] = branch.useState('title');
  const [body, setBody] = branch.useState('body');

  // Alternatively, you can connect to the state like this:
  // const [title, setTitle] = store.useState('notes', id, 'title');
  // const [body, setBody] = store.useState('notes', id, 'body');

  // Render
  return (
    <div>
      <input value={title} onChange={e => setTitle(e.target.value)} />
      <textarea value={body} onChange={e => setBody(e.target.value)} />
    </div>
  );
}

/**
 * The Note-function may also connect to the state a
 * bit higher up in the hierarchy:
 */
function Note({ id }: { id: string }) {
  // Lake states
  const [note, setNote] = store.useState('notes', id);

  // Render
  return (
    <div>
      <input
        value={note.title}
        onChange={e => setNote({ ...note, title: e.target.value })}
      />
      <textarea
        value={note.body}
        onChange={e => setNote({ ...note, body: e.target.value })}
      />
    </div>
  );
}
1.2.8

1 year ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.9

3 years ago

1.0.10

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago