2.0.0 • Published 1 year ago

forgo-state v2.0.0

Weekly downloads
235
License
MIT
Repository
github
Last release
1 year ago

forgo-state

Easy Application State Management for Forgo Apps using JavaScript Proxies.

Installation

npm i forgo-state

Defining application state variables

Start by defining one or more state variables using the defineState() API. These states can be bound to multiple components in the application.

import { bindToStates, defineState } from "forgo-state";

const mailboxState = defineState({
  messages: [],
  drafts: [],
  spam: [],
  unread: 0,
});

const signinState = defineState({
  username: "",
  lastActive: 0,
});

Binding components to your application state

Use bindToStates() to bind one or more states to any component. In the following example, whenever mailboxState or signinState changes, the bound component MailboxView is rerendered. Similarly, NotificationsBar is also bound to mailboxState.

function MailboxView() {
  const component = {
    render(props: any, args: ForgoRenderArgs) {
      return (
        <div>
          {mailboxState.messages.length ? (
            mailboxState.messages.map((m) => <p>{m}</p>)
          ) : (
            <p>There are no messages for {signinState.username}.</p>
          )}
        </div>
      );
    },
  };
  return bindToStates([mailboxState, signinState], component);
}

function NotificationsBar() {
  const component = {
    render() {
      return (
        <div>
          {mailboxState.unread > 0 ? (
            <p>You have {mailboxState.unread} notifications.</p>
          ) : (
            <p>There are no notifications.</p>
          )}
        </div>
      );
    },
  };
  return bindToStates([mailboxState], component);
}

You could update the state properties directly:

async function updateInbox() {
  const data = await fetchInboxData();
  // The next line causes a rerender of the MailboxView component
  mailboxState.messages = data;
}

Binding components to specific properties of the state

Sometimes, you're interested in rerendering only when a specific property of a state variable changes. There's another api for this, bindToStateProps().

Usage is similar. But instead of an array of states you're interested in, you'll have to pass an array of state, propertiesGetter tuples.

Here's an example:

function MailboxView() {
  const component = {
    render(props: any, args: ForgoRenderArgs) {
      return (
        <div>
          {mailboxState.messages.length ? (
            mailboxState.messages.map((m) => <p>{m}</p>)
          ) : (
            <p>There are no messages for {signinState.username}.</p>
          )}
        </div>
      );
    },
  };
  return bindToStateProps(
    // Render only if mailboxState.messages or mailboxState.drafts
    // or signinState.username changes.
    [
      [mailboxState, (state) => [state.messages, state.drafts]],
      [signinState, (state) => [state.username]],
    ],
    component
  );
}
2.0.0

1 year ago

1.3.2-beta.0

2 years ago

1.3.2

2 years ago

2.0.0-alpha.0

2 years ago

1.3.0

2 years ago

1.3.0-beta.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.42

2 years ago

1.0.40

2 years ago

1.0.41

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.26

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.11

3 years ago

1.0.10

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

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.44

3 years ago

0.0.43

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.36

3 years ago

0.0.35

3 years ago

0.0.34

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.27

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.26

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago