Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-24 - React Native List Filtering Memoization
**Learning:** React Native flat list filtering should always be memoized. If an expensive filter runs on every render (e.g. `const currentData = array.filter(...)`), any unrelated state update (like opening a modal or updating a boolean flag `notifsEnabled`) will trigger a full recalculation and drop frames on the JS thread.
**Action:** When working with long arrays of data fed into `FlatList` in React Native, ensure derived data filtering is wrapped in a `useMemo` hook with strict dependencies.
51 changes: 24 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions src/screens/FlightScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,21 @@ export default function FlightScreen() {
}, []);

const userShift = activeDay === 'today' ? shifts.today : shifts.tomorrow;
const selectedDate = activeDay === 'today' ? new Date() : (() => { const d = new Date(); d.setDate(d.getDate() + 1); return d; })();
const isSameDay = (d1: Date, d2: Date) =>
d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();

const currentData = (activeTab === 'arrivals' ? arrivals : departures).filter(item => {
const ts = activeTab === 'arrivals' ? item.flight?.time?.scheduled?.arrival : item.flight?.time?.scheduled?.departure;
return ts && isSameDay(new Date(ts * 1000), selectedDate);
});
// ⚡ Bolt Optimization: Memoize the flight filtering logic.
// Why: Prevents O(N) recalculations on unrelated state updates (e.g. notifications toggle),
// which can cause frame drops when the flight lists are large.
// Impact: Improves JS thread performance by skipping redundant filtering.
const currentData = useMemo(() => {
const selectedDate = activeDay === 'today' ? new Date() : (() => { const d = new Date(); d.setDate(d.getDate() + 1); return d; })();
const isSameDay = (d1: Date, d2: Date) =>
d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();

return (activeTab === 'arrivals' ? arrivals : departures).filter(item => {
const ts = activeTab === 'arrivals' ? item.flight?.time?.scheduled?.arrival : item.flight?.time?.scheduled?.departure;
return ts && isSameDay(new Date(ts * 1000), selectedDate);
});
}, [activeTab, activeDay, arrivals, departures]);

const renderFlight = useCallback(({ item }: { item: any }) => {
const flightNumber = item.flight?.identification?.number?.default || 'N/A';
Expand Down
Loading