All Projects โ†’ developit โ†’ Preact Cycle

developit / Preact Cycle

Licence: mit
โ™ป๏ธ Minimal functional Virtual DOM rendering using Preact ๐Ÿšฒ

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Preact Cycle

Typescript
The TypeScript template for Preact CLI
Stars: โœญ 68 (-43.33%)
Mutual labels:  preact
Nanocomponent Adapters
๐Ÿ”Œ - Convert a nanocomponent to a component for your favourite API or library (web components, (p)react, angular)
Stars: โœญ 94 (-21.67%)
Mutual labels:  preact
Tiny Atom
Pragmatic and concise state management.
Stars: โœญ 109 (-9.17%)
Mutual labels:  preact
Preact Cli Ssr
A quick demo for adding SSR to a Preact CLI app
Stars: โœญ 76 (-36.67%)
Mutual labels:  preact
Offline Gallery
๐ŸŽˆ A 16kb Preact & Redux based Progressive Web App that offers an offline gallery experience of external images.
Stars: โœญ 90 (-25%)
Mutual labels:  preact
Dataformsjs
๐ŸŒŸ DataFormsJS ๐ŸŒŸ A minimal JavaScript Framework and standalone React and Web Components for rapid development of high quality websites and single page applications.
Stars: โœญ 95 (-20.83%)
Mutual labels:  preact
Cloudflare Worker Preact Pwa
Cloudflare worker running a Preact Progressive Web App
Stars: โœญ 57 (-52.5%)
Mutual labels:  preact
Mobx Preact
MobX bindings for Preact
Stars: โœญ 117 (-2.5%)
Mutual labels:  preact
Colors App
๐ŸŽจ A PWA for copying values from popular color palettes. Supports HEX, RGB, and HSL formats.
Stars: โœญ 90 (-25%)
Mutual labels:  preact
Leo
Highly Extensible, Declarative Static Site Generator
Stars: โœญ 100 (-16.67%)
Mutual labels:  preact
Hueify
๐Ÿ’กSimple controller for your Philips Hue lights, based on web technologies.
Stars: โœญ 84 (-30%)
Mutual labels:  preact
Preact Todomvc
๐Ÿ’ฃ TodoMVC done in Preact. Under 6kb and fast.
Stars: โœญ 88 (-26.67%)
Mutual labels:  preact
Led Matrix Simulator
๐Ÿ”ฎ A simple HTML5 LED Matrix Simulator for fun
Stars: โœญ 96 (-20%)
Mutual labels:  preact
Chirrapp
Chirr App splits text into tweets and posts it as a thread
Stars: โœญ 75 (-37.5%)
Mutual labels:  preact
Razzle
โœจ Create server-rendered universal JavaScript applications with no configuration
Stars: โœญ 10,547 (+8689.17%)
Mutual labels:  preact
Enzyme Adapter Preact Pure
Preact adapter for the Enzyme UI testing library
Stars: โœญ 62 (-48.33%)
Mutual labels:  preact
Jetpack
๐Ÿš€ Jetpack โ€“ Webpack made more convenient.
Stars: โœญ 1,326 (+1005%)
Mutual labels:  preact
Preact Scroll Viewport
Preact Component that renders homogeneous children only when visible
Stars: โœญ 118 (-1.67%)
Mutual labels:  preact
Next Todos
200 lines realtime todos app powered by next.js, preact, jet, redux and now
Stars: โœญ 117 (-2.5%)
Mutual labels:  preact
Pimg
๐Ÿ“ท Mini Image Lazy Loader for P(R)eact and Vue
Stars: โœญ 97 (-19.17%)
Mutual labels:  preact

preact-cycle

NPM travis-ci

Minimal functional (-reactive) Virtual DOM rendering using Preact.


Simple Example

View this example on esnextb.in

import { render, h } from 'preact-cycle';
/** @jsx h */

const App = ({ value, mutation }) => (
  <div>
    <p>Value: { value }</p>
    <button onClick={ mutation('value', v => v+1) }>Increment</button>
  </div>
);

render(App, { value: 0 });

To-Do List Example

A simple example, where reducers are just pure functions. Note that TOGGLE mutates state in-place, which works fine but is discouraged.

View this example on CodePen

import { render, h } from 'preact-cycle';
/** @jsx h */

const ADD = ({ text, todos, ...state }) => ({
  todos: todos.concat({ text }),
  text: '',
  ...state
});

const TOGGLE = (state, todo) => {
  todo.done = !todo.done;
  return state;
};

const REMOVE = ({ todos, ...state }, todo) => ({
  todos: todos.filter( t => t!==todo ),
  ...state
});


const TodoList = ({ text, todos, mutate, mutation }) => (
  <div>
    <form onSubmit={mutation(ADD)} action="javascript:">
      <input value={text} onInput={e => mutate('text', e.target.value)} />
      <button action="submit">Add</button>
    </form>
    <ul>
      { todos.map( todo => (
        <li onClick={mutation(TOGGLE, todo)}>
          <input type="checkbox" checked={todo.done} readonly />
          <p>{ todo.text }</p>
          <a onClick={mutation(REMOVE, todo)}>โœ•</a>
        </li>
      ))}
    </ul>
  </div>
);

render(TodoList, { todos: [] }, document.body);

Component-Based Example

Normal preact components still work great with preact-cycle. As of v0.4, mutate() and mutation() are conveniently available as context properties, which means they are automatically passed down through the VDOM tree. For pure functional components, context is simply passed as a second argument.

A component-based variant of the previous To-Do List example follows, using pure functions and context.

import { h, render } from 'preact-cycle';
/** @jsx h */


/** initial data to populate the store */
const INITIAL_DATA = {
  todos: [
    { text:'Type some text' },
    { text:'...then hit [enter]' },
    { text:'Now you\'re productive!' }
  ]
};

/** Appends a new todo item */
const ADD = ({ todos, text, ...state }) => ({
  todos: todos.concat({ text }),
  text: '',
  ...state
});

/** Remove the given todo item */
const REMOVE = ({ todos, ...state }, todo) => ({
  todos: todos.filter(t => t!==todo),
  ...state
});

/** Toggles the given todo item as done */
const TOGGLE = (state, todo) => {
  todo.done = !todo.done;
};


/** a simple helper to derive a mutated value from an event */
let fromEvent = (prev, e) => e.target.value;


/** The todo list app */
const App = ({ text, todos }) => (
  <div id="app">
    <Form text={text} />
    <ul>{ todos.map( todo => (
      <Item todo={todo} />
    )) }</ul>
  </div>
);

/** New todo entry form */
const Form = ({ text }, { mutation }) => (
  <form onSubmit={mutation(ADD)} action="javascript:">
    <input placeholder="New item..."
      value={text}
      onInput={mutation('text', fromEvent)} />
  </form>
);

/** A single todo list item */
const Item = ({ todo }, { mutation }) => (
  <li onClick={mutation(TOGGLE, todo)} class={{ done: todo.done }}>
    <input type="checkbox" checked={todo.done} readonly />
    <a onClick={mutation(REMOVE, todo)}>โœ•</a>
    <p>{ todo.text }</p>
  </li>
);

// Kick off the cycle!
render(App, INITIAL_DATA, document.body);

License

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