All Projects → vaukalak → react-mlyn

vaukalak / react-mlyn

Licence: MIT license
react bindings to mlyn

Programming Languages

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

Projects that are alternatives of or similar to react-mlyn

atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+184.21%)
Mutual labels:  state-management, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+18421.05%)
Mutual labels:  state-management, react-hooks
react-store
A library for better state management in react hooks world
Stars: ✭ 34 (+78.95%)
Mutual labels:  state-management, react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (+163.16%)
Mutual labels:  state-management, react-hooks
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (+684.21%)
Mutual labels:  state-management, immutability
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-31.58%)
Mutual labels:  state-management, react-hooks
Mobx Keystone
A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
Stars: ✭ 284 (+1394.74%)
Mutual labels:  state-management, immutability
east-store
east-store is a state manager with easiest api that based hooks and immer.
Stars: ✭ 18 (-5.26%)
Mutual labels:  state-management, react-hooks
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (+600%)
Mutual labels:  state-management, immutability
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+23715.79%)
Mutual labels:  state-management, react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-31.58%)
Mutual labels:  state-management, react-hooks
rex-state
Convert hooks into shared states between React components
Stars: ✭ 32 (+68.42%)
Mutual labels:  state-management, react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+242.11%)
Mutual labels:  state-management, react-hooks
hookstore
Hook based and lightweight centralized state management for React.
Stars: ✭ 28 (+47.37%)
Mutual labels:  state-management, react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (+0%)
Mutual labels:  state-management, react-hooks
use-simple-store
🏬 Simple state management using React Hook
Stars: ✭ 21 (+10.53%)
Mutual labels:  state-management, react-hooks
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (+47.37%)
Mutual labels:  state-management, react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1194.74%)
Mutual labels:  state-management, react-hooks
React Recollect
State management for React
Stars: ✭ 411 (+2063.16%)
Mutual labels:  state-management, immutability
immutable-cursor
👊 Immutable cursors incorporating the Immutable.js interface over a Clojure-inspired atom
Stars: ✭ 58 (+205.26%)
Mutual labels:  state-management, immutability

react-mlyn

React bindings to mlyn

Goals of this library:

  • Reduce re-renders of the application due to subscription based model (update only components that changed).
  • Allow 2 way binding, without violating Unidirectional Data Flow.
  • Reduce react app / components boilerplate.
  • Reduce the amount of properties passed to components props / context, and make components reuse easier.

Installation

Install both react-mlyn and mlyn.

yarn add react-mlyn mlyn

or

npm i -S react-mlyn mlyn

Eamples

Quick start

import Mlyn, { useSubject, seal } from "react-mlyn";

const App = seal(() => {
  const firstName$ = useSubject("Barbara");
  const lastName$ = useSubject("Radzivil");
  return (
    <div>
      <div>First name:</div>
      <Mlyn.Input bindValue={firstName$} />
      <div>Last name:</div>
      <Mlyn.Input bindValue={lastName$} />
      <div>Full name:</div>
      <Mlyn.Div>{() => `${firstName$()} ${lastName$()}`}</Mlyn.Div>
    </div>
  );
});

Documentation

Components

Show

Can show / hide an element on a condition, without re-rendering host component:

<Show when={() => subject() > 1}>
  {() => (
      <div>More than 1</div>
  )}
</Show>

For

Used to display a collection of elements, by providing items to render and key extractor.

<For each={state$.todos}>
  {(todo$, index$) => (
    <div>
      <Mlyn.Input type="checkbox" bindChecked={todo$.done} />
      <Mlyn.Input bindValue={todo$.title} />
      <button onClick={() => removeItem(index$())}>x</button>
    </div>
  )}
</For>

Hooks

useSubject: Creates a memoized subject, by passing to it initial state:

const subject = useSubject({ x: 1 });

useMlynEffect: Simlar to reacts useEffect, however doesn't require dependencies, cause it's automatically subscribed to to mlyn bindings. Please note, that invocation of hook callback doesn't mean that component has been re-rendered.

const usePersist = (key: string, subject: Sybject<any>) => {
    useEffect(() => {
        const persisted = localStorage.getItem(key);
        if (persisted) {
            subject(JSON.parse(persisted));
        }
    }, []); // will perform once
    useMlynEffect(() => {
        localStorage.setItem(key, JSON.stringify(subject());
    });
}

(Advanced) useSubjectValue: creates react state entry binded to the subject value. This hook will cause component re-render, which might can be required in components like For.

const subject = useSubject(0);
const value = useSubjectValue(subject); // starts with 0
useEffect(() => {
    subject(1);
    // component will rerender
    // `value` will become 1
}, []);

Since subject has been invoked during execution of useMlynEffect callback, this callback will be reinvoked on every change of subject value.

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].