All Projects → calmm-js → Karet

calmm-js / Karet

Licence: mit
Karet is a library that allows you to embed Kefir observables into React VDOM

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Karet

Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-44.44%)
Mutual labels:  reactive, observables, jsx
Vidom
Library to build UI based on virtual DOM
Stars: ✭ 408 (+403.7%)
Mutual labels:  reactive, jsx
Awesome Rxjs
A collection of awesome RxJS resources
Stars: ✭ 314 (+287.65%)
Mutual labels:  reactive, observables
Nerv
A blazing fast React alternative, compatible with IE8 and React 16.
Stars: ✭ 5,409 (+6577.78%)
Mutual labels:  vdom, jsx
reactive-search
Incremental search using React and RxJS
Stars: ✭ 15 (-81.48%)
Mutual labels:  reactive, incremental
zaftig
~2kB css in js: z`display flex` // .zjsdkk43-1
Stars: ✭ 15 (-81.48%)
Mutual labels:  jsx, vdom
Vhtml
Render JSX/Hyperscript to HTML strings, without VDOM 🌈
Stars: ✭ 556 (+586.42%)
Mutual labels:  vdom, jsx
joltik
A really small VDOM library
Stars: ✭ 40 (-50.62%)
Mutual labels:  jsx, vdom
Platform
Reactive libraries for Angular
Stars: ✭ 7,020 (+8566.67%)
Mutual labels:  reactive, observables
Muve
Muve is a micro library for building interactive javascript applications.
Stars: ✭ 11 (-86.42%)
Mutual labels:  vdom, jsx
Preact
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Stars: ✭ 30,527 (+37587.65%)
Mutual labels:  vdom, jsx
whatsup
Reactive framework, simple, fast, easy to use!
Stars: ✭ 115 (+41.98%)
Mutual labels:  reactive, jsx
alef-component
Alef Component for Modern Web Apps.
Stars: ✭ 46 (-43.21%)
Mutual labels:  reactive, jsx
Toy Rx
A tiny implementation of RxJS that actually works, for learning
Stars: ✭ 290 (+258.02%)
Mutual labels:  reactive, observables
awesome-solid-js
Curated resources on building sites with SolidJS, a brand new way(now 1.0) to build Javascript based interactive web applications. A very close looking cousin to React/JSX by syntax, and to Svelte by few important principles(compiler and fine-grained reactivity), it's a highly optimised way to deliver web applications with best-in-class performa…
Stars: ✭ 317 (+291.36%)
Mutual labels:  reactive, jsx
Hyperawesome
A curated list of awesome projects built with Hyperapp & more.
Stars: ✭ 446 (+450.62%)
Mutual labels:  vdom, jsx
callbag-subscribe
A callbag sink (listener) that connects an Observer a-la RxJS. 👜
Stars: ✭ 17 (-79.01%)
Mutual labels:  reactive, observables
purescript-outwatch
A functional and reactive UI framework based on Rx and VirtualDom
Stars: ✭ 33 (-59.26%)
Mutual labels:  reactive, vdom
Solid
A declarative, efficient, and flexible JavaScript library for building user interfaces.
Stars: ✭ 13,115 (+16091.36%)
Mutual labels:  reactive, jsx
Wonders
🌈 Declarative JavaScript framework to build command-line applications.
Stars: ✭ 34 (-58.02%)
Mutual labels:  vdom, jsx

Karet · Gitter GitHub stars npm

Karet is a library that allows you to embed Kefir properties into React Virtual DOM. Embedding observable properties into VDOM has the following benefits:

  • It allows you to use only functional components, because you can then use observables for managing state and component lifetime, leading to more concise code.
  • It helps you to use React in an algorithmically efficient way:
    • The body of a functional component is evaluated only once each time the component is mounted.
    • Only elements that contain embedded properties are rerendered when changes are pushed through observables. An update to a deeply nested VDOM element can be an O(1) operation.

Using Karet couldn't be simpler. Usually you just import * as React from 'karet' and you are good to go.

npm version Gitter Build Status Code Coverage

Contents

Tutorial

To use Karet, you simply import it as React:

import * as React from 'karet'

and you can then write React components:

const App = () => (
  <div>
    <h1>What is the date and time</h1>
    {Kefir.interval(1000)
      .toProperty(() => {})
      .map(() => new Date().toString())}
  </div>
)

with VDOM that can have embedded Kefir properties. This works because Karet exports an enhanced version of createElement.

Note that the result, like the date and time display above, is just a React component. If you export it, you can use it just like any other React component and even in modules that do not import karet.

Here is a live example in CodeSandbox.

More links to live examples in the Calmm documentation Wiki.

Reference

React exports passed through

Karet passes through the following exports from React:

  • Children as is. Note that with observable properties in children these functions may not do exactly what you want and you might want to lift them.
  • Fragment as is. It should work without problems.
  • createContext as is. Note that with Karet it is preferable to put observable properties into the context and let changes propagate through them rather than update the context. Also note that neither the provider nor the consumer are lifted by default. Lifting the consumer will likely cause no issues, but lifting the provider would eliminate observables from the value property and could cause problems. If you need to have observable children inside the provider, you can wrap the children inside a Fragment. See the CodeSandbox examples
    • Exam Events Form where the context is used to transmit the language as an observable atom, and
    • Form using Context where context is used to transmit form properties to form elements.
  • createElement which lifts Kefir properties in fragments and built-in HTML elements.
  • forwardRef as is.
  • useContext as is.

Notably the following are not exported:

  • Component and PureComponent, because with Karet you really don't need them and the render method can cause undesired component remounting when used with observable properties embedded into VDOM.
  • cloneElement does not work out of the box with elements containing Kefir properties. It should be possible to support it, however.
  • createRef is not exported, because Karet Util provides an alternative that works better with observable properties.
  • Suspense and lazy are not needed since import() returns a promise and you can just convert that to an observable property like this.
  • Most React hooks are not exported because they are not typically needed in Karet components. State should be handled in atoms. Effects can already be handled using observable properties as children or props.

karet-lift attribute

Karet only lifts built-in HTML elements and fragments implicitly. The karet-lift attribute on a non-primitive element instructs Karet to lift the element.

For example, you could write:

import Select from 'react-select'
import * as React from 'karet'

// ...

const ReactSelect1 = ({value}) => (
  <Select
    karet-lift
    name="form-field-name"
    value={value}
    options={options}
    onChange={o => value.set(o && o.value)}
  />
)

to be able to use Select from React Select with embedded Kefir Atoms.

Here is a live example in CodeSandbox.

fromClass(Component)

Karet only lifts built-in HTML elements and fragments implicitly. fromClass allows one to create lifted version of a given React component.

For example, you could write:

import Select from 'react-select'
import * as React from 'karet'

const SelectLifted = React.fromClass(Select)

const ReactSelect2 = ({value}) => (
  <SelectLifted
    name="form-field-name"
    value={value}
    options={options}
    onChange={o => value.set(o && o.value)}
  />
)

to be able to use Select from React Select with embedded Kefir Atoms.

Here is a live example in CodeSandbox.

Known gotchas

The React inline elements transform is incompatible with Karet, because it bypasses React.createElement. OTOH, the React constant elements transform works just fine with Karet.

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