Skip to content

Instantly share code, notes, and snippets.

@developit
Last active December 2, 2015 14:40
Show Gist options
  • Save developit/02d67ac238290febe463 to your computer and use it in GitHub Desktop.
Save developit/02d67ac238290febe463 to your computer and use it in GitHub Desktop.
Preliminary Preact Docs

This is a first crack at generated docs for Preact.

attributes

children

Component

Base Component class, for he ES6 Class method of creating Components

Examples

class MyFoo extends Component {
	render(props, state) {
		return <div />;
	}
}

linkState

Returns a function that sets a state property when called. Calling linkState() repeatedly with the same arguments returns a cached link function.

Provides some built-in special cases:

- Checkboxes and radio buttons link their boolean `checked` value
- Inputs automatically link their `value` property
- Event paths fall back to any associated Component if not found on an element
- If linked value is a function, will invoke it and use the result

Parameters

  • key string The path to set - can be a dot-notated deep key
  • eventPath [string] If set, attempts to find the new state value at a given dot-notated path within the object passed to the linkedState setter.

Examples

Update a "text" state value when an input changes:
	<input onChange={ this.linkState('text') } />
Set a deep state value on click
	<button onClick={ this.linkState('touch.coords', 'touches.0') }>Tap</button

Returns function linkStateSetter(e)

render

Accepts props and state, and returns a new Virtual DOM tree to build. Virtual DOM is generally constructed via JSX.

Parameters

  • props object Props (eg: JSX attributes) received from parent element/component
  • state object The component's current state

Returns **** VNode

setState

Update component state by copying properties from state to this.state.

Parameters

  • state object A hash of state properties to update with new values

shouldComponentUpdate

Returns a boolean value indicating if the component should re-render when receiving the given props and state.

Parameters

  • props object
  • state object

h

Parameters

  • nodeName
  • attributes
  • args ...

Examples

/** @jsx h *\/
 import { render, h } from 'preact';
 render(<span>foo</span>, document.body);

hooks

Global hook methods

Parameters

  • $0
    • $0.attributes

vnode

Processes all created VNodes.

Parameters

  • vnode VNode A newly-created VNode to normalize/process
  • $0
    • $0.attributes

nextProps

nodeName

options

Global options

syncComponentUpdates

If true, prop changes trigger synchronous component updates.

props

render

Render JSX into a parent Element.

Parameters

  • vnode VNode A (JSX) VNode to render
  • parent Element DOM element to render into
  • merge [Element] Attempt to re-use an existing DOM tree rooted at merge

Examples

// render a div into <body>:
render(<div id="hello">hello!</div>, document.body);
// render a "Thing" component into #foo:
const Thing = ({ name }) => <span>{ name }</span>;
render(<Thing name="one" />, document.querySelector('#foo'));

rerender

Trigger all queued component renders.

setComponentProps

Set a component's props (generally derived from JSX attributes).

Parameters

  • props Object
  • opts [Object]
    • opts.renderSync [boolean] If true and options.syncComponentUpdates is true, triggers synchronous rendering. (optional, default false)
    • opts.render [boolean] If false, no render will be triggered. (optional, default true)
  • component

state

VNode

Virtual DOM Node

Parameters

  • nodeName
  • attributes
  • children
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment