Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify usage with integrations #101

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,38 @@ import { html, render } from 'https://unpkg.com/htm/preact/standalone.mjs'

## Usage

Since `htm` is a generic library, we need to tell it what to "compile" our templates to.
If you're using Preact or React, we've included off-the-shelf bindings to make your life easier.
They also have the added benefit of sharing a template cache across all modules.

The target should be a function of the form `h(type, props, ...children)` _([hyperscript])_, and can return anything.
```js
import { render } from 'preact';
import { html } from 'htm/preact';
render(html`<a href="/">Hello!</a>`, document.body);
```

Similarly, for React:

```js
import ReactDOM from 'react-dom';
import { html } from 'htm/react';
ReactDOM.render(html`<a href="/">Hello!</a>`, document.body);
```

### Advanced Usage

Since `htm` is a generic library, so we need to tell it what to "compile" our templates to.
developit marked this conversation as resolved.
Show resolved Hide resolved
You can bind `htm` to any function of the form `h(type, props, ...children)` _([hyperscript])_.
This function can return anything - `htm` never looks at the return value.

Here's an example `h()` function that returns tree nodes:

```js
// this is our hyperscript function. for now, it just returns a description object.
function h(type, props, ...children) {
return { type, props, children };
}
```

To use that `h()` function, we need to create our own `html` tag function by binding `htm` to our `h()` function:
To use our custom `h()` function, we need to create our own `html` tag function by binding `htm` to our `h()` function:

```js
import htm from 'htm';
Expand Down