2015-12-26 00:49:18 +00:00
|
|
|
---
|
2016-01-07 04:47:48 +00:00
|
|
|
title: Deku v2
|
2015-12-26 00:49:18 +00:00
|
|
|
category: JavaScript libraries
|
2020-07-05 13:13:06 +00:00
|
|
|
intro: |
|
|
|
|
Quick reference for [Deku](https://www.npmjs.com/package/deku), a minimal virtual DOM library.
|
2015-12-26 00:49:18 +00:00
|
|
|
---
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
### Components
|
2015-12-26 00:49:18 +00:00
|
|
|
|
|
|
|
```js
|
2016-01-07 04:47:48 +00:00
|
|
|
/** @jsx element */
|
|
|
|
import { element } from 'deku'
|
|
|
|
|
|
|
|
function render ({ props, children, context, path }) {
|
|
|
|
// props = properties object
|
|
|
|
// children = children array
|
|
|
|
// path = path to current component (like 0.1.5.2)
|
|
|
|
// context = common properties in all components
|
|
|
|
return (
|
|
|
|
<div class='App' hidden={props.hidden} color={context.theme.color}>
|
|
|
|
{children}
|
|
|
|
</div>
|
2015-12-26 00:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
function onCreate ({ props, dispatch, path }) { ... }
|
|
|
|
function onUpdate ({ props, dispatch, path }) { ... }
|
|
|
|
function onRemove ({ props, dispatch, path }) { ... }
|
|
|
|
// actually { children, props, path, context }
|
2015-12-26 00:49:18 +00:00
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
export default { render, onCreate, onRemove }
|
2015-12-26 00:49:18 +00:00
|
|
|
```
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
### Rendering
|
2015-12-26 00:49:18 +00:00
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
```js
|
|
|
|
import { createStore } from 'redux'
|
|
|
|
import { dom, element } from 'deku'
|
2015-12-26 00:49:18 +00:00
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
// Create a Redux store to handle all UI actions and side-effects
|
|
|
|
let store = createStore(reducer)
|
2015-12-26 00:49:18 +00:00
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
// Create a renderer that can turn vnodes into real DOM elements
|
|
|
|
let render = createRenderer(document.body, store.dispatch)
|
2015-12-26 00:49:18 +00:00
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
// Update the page and add redux state to the context
|
2020-07-05 13:13:06 +00:00
|
|
|
render(<MyButton>Hello World!</MyButton>, store.getState())
|
2015-12-26 00:49:18 +00:00
|
|
|
```
|