Skip to main content

react()

Starts a new effect scheduler, scheduling the effect immediately.

Returns a function that can be called to stop the scheduler.

Example​

const color = atom('color', 'red')
const stop = react('set style', () => {
divElem.style.color = color.value
})
color.set('blue')
// divElem.style.color === 'blue'
stop()
color.set('green')
// divElem.style.color === 'blue'

Also useful in React applications for running effects outside of the render cycle.

Example​

useEffect(() => react('set style', () => {
divRef.current.style.color = color.value
}), [])

Signature​

react(name: string, fn: Function, options?: EffectSchedulerOptions): Function;

Parameters​

NameType
namestring
fn(lastReactedEpoch: number) => any
options?EffectSchedulerOptions

Returns​

Function

Signature​

(): void;

Returns​

void

Defined in: signia/src/EffectScheduler.ts:197