All Projects → ktripaldi → temperjs

ktripaldi / temperjs

Licence: MIT license
State management for React, made simple.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to temperjs

mutable
State containers with dirty checking and more
Stars: ✭ 32 (+113.33%)
Mutual labels:  flux, state-management, state
ReduxSimple
Simple Stupid Redux Store using Reactive Extensions
Stars: ✭ 119 (+693.33%)
Mutual labels:  state-management, state, selectors
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+3680%)
Mutual labels:  flux, state-management, state
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+260%)
Mutual labels:  state-management, state, recoiljs
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (+540%)
Mutual labels:  flux, state-management, state
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+333.33%)
Mutual labels:  flux, state-management, state
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+4260%)
Mutual labels:  flux, state-management, state
Stockroom
🗃 Offload your store management to a worker easily.
Stars: ✭ 1,745 (+11533.33%)
Mutual labels:  flux, state-management
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+13080%)
Mutual labels:  flux, state
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+1240%)
Mutual labels:  flux, state
enform
Handle React forms with joy 🍿
Stars: ✭ 38 (+153.33%)
Mutual labels:  state-management, state
Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (+626.67%)
Mutual labels:  flux, state
Nucleo
🏋️‍♀️ Nucleo is a strongly typed and predictable state container library for JavaScript ecosystem written in TypeScript
Stars: ✭ 109 (+626.67%)
Mutual labels:  flux, state-management
Retalk
🐤 The Simplest Redux
Stars: ✭ 168 (+1020%)
Mutual labels:  flux, state
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (+613.33%)
Mutual labels:  flux, state
react-globally
Gives you setGlobalState, so you can set state globally
Stars: ✭ 22 (+46.67%)
Mutual labels:  state-management, state
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (+580%)
Mutual labels:  flux, state-management
Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (+1280%)
Mutual labels:  flux, state-management
mafuba
Simple state container for react apps.
Stars: ✭ 20 (+33.33%)
Mutual labels:  flux, state
rex-state
Convert hooks into shared states between React components
Stars: ✭ 32 (+113.33%)
Mutual labels:  state-management, state

Temper · GitHub license

alt text

Table of contents

Getting Started

This readme is meant to get you familiar with the Temper way of doing things. If you're looking for something specific, please visit the documentation site. If you're just starting out with Temper, read on!

For the purpose of this guide, we'll create a simple counter that prints You've reached the target! when you get to the value of 5.

Create React App

Temper is a state management library for React, so you need to have React installed and running to use Temper. The easiest and recommended way for bootstrapping a React application is to use Create React App:

npx create-react-app my-app

npx is a package runner tool that comes with npm 5.2+ and higher, see instructions for older npm versions.

For more ways to install Create React App, see the official documentation.

Installation

Using npm:

npm install temperjs

Using yarn:

yarn add temperjs

withTemper

If you want to use Temper states, you need to wrap your component (preferably the root component) with the hoc withTemper.

import React from 'react';
import { withTemper } from 'temperjs';

function App() {
  return <Counter />
}

export default withTemper(App);

We'll implement the Counter component in the following section.

Traits

Temper states are called Traits. Traits are globally shared units of state that components can subscribe to. Traits can be read and written from any component in the application tree. Subscribed components will rerender everytime the Trait value changes.

If you want to set a Trait, use can use the action setTrait:

import React from 'react';
import { withTemper, setTrait } from 'temperjs';

function App() {
  setTrait('count', 0);
  return <Counter />
}

export default withTemper(App);

If you need to read from and write to a Trait, you can use the hook useTrait:

import React from 'react';
import { useTrait } from 'temperjs';

function Counter() {
  const [count, setCount] = useTrait('count');

  function incrementCounter() {
    setCount(({ value }) => value + 1);
  }
  function decrementCounter() {
    setCount(({ value }) => value - 1);
  }

  return (
    <div>
      <p>{count}</p>
      <button onClick={incrementCounter}>Increment</button>
      <button onClick={decrementCounter}>Decrement</button>
    </div>
  );
}

export default Counter;

Selectors

Selectors are derived Traits. You can think of selectors as the output of passing a state to a pure function that executes some logic based on that state.

If you want to create a selector, you can use the following syntax:

import React from 'react';
import { withTemper, setTrait } from 'temperjs';

function App() {
  // This is a simple Trait
  setTrait('count', 0);
  // This is a selector Trait
  setTrait('isTargetReached', ({ get }) => get('count') >= 5);

  return <Counter />
}

export default withTemper(App);

⚠️ CAUTION:

Selectors permanently depend on their reference Trait. When the reference Trait changes, the selector value is updated automatically.

Therefore, you cannot manually update selectors once set, since their value tightly depends on their base Trait.

Nested Traits

Temper encourages you to wrap related Traits in a single object. When a Trait is an object, each attribute will become a new Trait that is individually updatable and subscribable:

import React from 'react';
import { withTemper, setTrait } from 'temperjs';

function App() {
  setTrait('counter', {
    count: 0,
    isTargetReached: ({ get }) => get('counter.count') >= 5
  });

  return <Counter />
}

export default withTemper(App);

If you just need to read a Trait, you can use the hook useTraitValue (nested Traits are referenced with the dot notation.):

import React from 'react';
import { useTrait, useTraitValue } from 'temperjs';

function Counter() {
  const [count, setCount] = useTrait('counter.count');
  const isTargetReached = useTraitValue('counter.isTargetReached');

  function incrementCounter() {
    setCount(({ value }) => value + 1);
  }
  function decrementCounter() {
    setCount(({ value }) => value - 1);
  }

  return (
    <div>
      <p>{count} { isTargetReached && (<span>You've reached the target!</span>)}</p>
      <button onClick={incrementCounter}>Increment</button>
      <button onClick={decrementCounter}>Decrement</button>
    </div>
  );
}

export default Counter;

Wrapping things up

Run the Counter on CodeSandbox.

API Documentation

Have a look at the documentation site for more details on Temper.

Licence

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].