This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Deku Functional Virtual DOM Cheat Sheet

A quick reference guide for Deku v2, a minimalist functional virtual DOM library: JSX components, lifecycle hooks, and Redux store integration.

· Published

Deku is an elegant, functional virtual DOM library for building interactive user interfaces with plain JavaScript functions.

Functional Components

/** @jsx element */
import { element } from 'deku';

function render({ props, children, context, path }) {
  // props: Component property attributes
  // children: Array of nested child elements
  // context: Globally shared state/theme
  // path: Unique string identifier for component instance in tree
  return (
    <div class="card" hidden={props.hidden} style={{ color: context.theme.color }}>
      <h2>{props.title}</h2>
      {children}
    </div>
  );
}

function onCreate({ props, dispatch, path }) {
  // Triggered on component mount
}

function onUpdate({ props, nextProps, dispatch }) {
  // Triggered when props change
}

function onRemove({ props, dispatch }) {
  // Triggered on component unmount
}

export default { render, onCreate, onUpdate, onRemove };

Rendering & Redux Integration

import { createStore } from 'redux';
import { dom, element } from 'deku';
import App from './app';

const store = createStore(reducer);

// Bind renderer to DOM mounting container
const render = dom.createRenderer(document.getElementById('root'), store.dispatch);

// Subscribe to store updates
function update() {
  render(<App state={store.getState()} />, { theme: { color: '#333' } });
}

store.subscribe(update);
update();

Related topics