Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@react-spring/rafz
Advanced tools
react-spring's fork of rafz one frameloop to rule them all
@react-spring/rafz is a utility for managing requestAnimationFrame loops in React applications. It provides a simple and efficient way to handle animations and other frame-based updates.
Basic Usage
This feature allows you to schedule a function to run on the next animation frame using the `raf` function.
import { raf } from '@react-spring/rafz';
raf(() => {
console.log('This will run on the next animation frame');
});
Looping
This feature demonstrates how to create a continuous loop that runs on every animation frame.
import { raf } from '@react-spring/rafz';
let count = 0;
const loop = () => {
console.log('Frame:', count++);
raf(loop);
};
raf(loop);
Canceling a Frame
This feature shows how to cancel a scheduled frame using the `cancel` function.
import { raf, cancel } from '@react-spring/rafz';
const id = raf(() => {
console.log('This will not run');
});
cancel(id);
The `raf` package is a simple polyfill for requestAnimationFrame. It provides a basic interface for scheduling functions to run on the next animation frame. Unlike @react-spring/rafz, it does not offer additional utilities for managing loops or canceling frames.
The `framesync` package is a lightweight utility for managing frame-based updates. It offers a similar API to @react-spring/rafz, including functions for scheduling and canceling frames. However, it is more focused on providing a low-level interface for frame synchronization.
The `react-use` package is a collection of React hooks, including hooks for managing animations and frame-based updates. It provides a higher-level abstraction compared to @react-spring/rafz, making it easier to integrate with React components.
Coordinate requestAnimationFrame
calls across your app and/or libraries.
ReactDOM.unstable_batchedUpdates
)
import { raf } from '@react-spring/rafz'
// Schedule an update
raf(dt => {})
// Start an update loop
raf(dt => true)
// Cancel an update
raf.cancel(fn)
// Schedule a mutation
raf.write(() => {})
// Before any updates
raf.onStart(() => {})
// Before any mutations
raf.onFrame(() => {})
// After any mutations
raf.onFinish(() => {})
// Set a timeout that runs on nearest frame
raf.setTimeout(() => {}, 1000)
// Use a polyfill
raf.use(require('@essentials/raf').raf)
// Get the current time
raf.now() // => number
// Set how you want to control raf firing
raf.frameLoop = 'demand' | 'always'
update
phase is for updating JS state (eg: advancing an animation).write
phase is for updating native state (eg: mutating the DOM).write
phase.onFrame
phase.true
to run again next frame.raf.cancel
function only works with raf
handlers.raf.sync
to disable scheduling in its callback.raf.batchedUpdates
to avoid excessive re-rendering in React.
raf.throttle
Wrap a function to limit its execution to once per frame. If called more than once in a single frame, the last arguments are used.
let log = raf.throttle(console.log)
log(1)
log(2) // nothing logged yet
raf.onStart(() => {
// "2" is logged by now
})
// Cancel a pending call.
log.cancel()
// Access the wrapped function.
log.handler
FAQs
react-spring's fork of rafz one frameloop to rule them all
The npm package @react-spring/rafz receives a total of 1,103,389 weekly downloads. As such, @react-spring/rafz popularity was classified as popular.
We found that @react-spring/rafz demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.