Avoid triggering expensive operations (queries, API calls, computations) on every user input or state change. Instead, use appropriate triggers that balance responsiveness with performance.
Key strategies:
onBlur
instead of onChange
for expensive operations that don’t need immediate feedbackuseEffect
to event handlers when the data is only needed in response to specific user actionsExample from the discussions:
// Instead of triggering expensive queries on every change:
<LemonInput
onChange={(value) => expensiveQuery(value)} // Causes UI jumpiness
/>
// Use onBlur or save buttons:
<LemonInput
onBlur={(value) => expensiveQuery(value)} // Better performance
/>
// Or move from useEffect to event handlers:
// Instead of:
useEffect(() => {
if (needsData) {
fetchExpensiveData()
}
}, [dependency])
// Use:
const handleUserAction = () => {
if (needsData) {
fetchExpensiveData() // Only when actually needed
}
}
This approach reduces unnecessary resource utilization and prevents performance bottlenecks that degrade user experience.
Enter the URL of a public GitHub repository