Skip to content

RFC: Hooks #87

@thysultan

Description

@thysultan

Similar to React Hooks. RFC discussion.

The differences being

  1. The use of a single setter/getter stream instead of a tuple. So value = use(value) instead of [value, setValue] = use(value). This allows us to use live values to avoid the age old problem of stale data. That is value() would always return the current value.
  2. The unification of hooks and effects into a single use API. i.e use(value) and use(value, callback) instead of useState and useEffect.
  3. Support for unmount animations as can be archived with componentWillUnmount. Without which hooks wouldn't have full feature parity with lifecycle methods.
  4. ...and passing [dependencies], props, state, context to the effect callback to effectively avoid being tied to closures.

An example of the aforementioned might look like the following:

import {h, use, render} from 'dyo'

function Counter (props, {refs, value = use(0)}, context) {
	try {
		return h('div', {ref: refs},
			h('button', {onClick: e => value(value + 1)}, '+'),
			h('button', {onClick: e => value(value - 1)}, '-'),
			h('h1', {}, value)
		)
	} finally {
		useAnimation(value)
	}
}

function useAnimation (value) {
	return use([value], ([value], props, {refs}, context) => {
		return () => {
			return refs.current.animate([], {}).finished
		}
	})
}

Point 3 arises the question of how do we handle the case when multiple effects try to register unmount animations: Do we 1. Register all the animations? or 2. Register the last animation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions