Similar to React Hooks. RFC discussion.
The differences being
- 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.
- The unification of hooks and effects into a single
use API. i.e use(value) and use(value, callback) instead of useState and useEffect.
- Support for unmount animations as can be archived with
componentWillUnmount. Without which hooks wouldn't have full feature parity with lifecycle methods.
- ...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?
Similar to React Hooks. RFC discussion.
The differences being
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 isvalue()would always return the current value.useAPI. i.euse(value)anduse(value, callback)instead ofuseStateanduseEffect.componentWillUnmount. Without which hooks wouldn't have full feature parity with lifecycle methods.[dependencies], props, state, contextto the effect callback to effectively avoid being tied to closures.An example of the aforementioned might look like the following:
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?