Giter VIP home page Giter VIP logo

react-mlyn's Introduction

react-mlyn

React bindings to mlyn

Goals of this library:

  • Bring fine grained reactivity to React world (update only components that changed).
  • Allow 2 way binding, without violating Unidirectional Data Flow.
  • 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

Develop

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} getKey={({ createdAt }) => createdAt}>
  {(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.

react-mlyn's People

Contributors

vaukalak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.