Spitha is a performance-enhancing wrapper component for React and Next.js, written in TypeScript. It optimizes component rendering using an internal system of memoization, batched updates, and asynchronous lazy rendering. The component is designed to integrate seamlessly into any modern React project and supports native ChakraUI styling.
Traditional React components tend to re-render unnecessarily, especially in complex UI trees with state-heavy children or repeated API calls. This affects performance in data-intensive applications such as dashboards or graph-based UIs. Spitha was designed to address this bottleneck by intelligently controlling when and how components render and refresh — without requiring manual memoization or architectural overhauls.
To prevent function re-creation across renders, Spitha uses:
const stableCallback = useCallback(() => { ... }, []);
This ensures downstream child components do not re-render unnecessarily due to new references, preserving render time and keeping React’s reconciliation tree shallow.
When the user scrolls past a Spitha-wrapped component, it triggers background API fetching using a custom intersection observer and asynchronous logic. This ensures network activity occurs during user inactivity:
if (isOutOfView && !loading) {
fetchData().then(setData);
}
React’s native event batching is extended in Spitha with a `threshold` prop, which defines the scroll distance from the component before re-render is allowed. For instance:
<Spitha threshold={0.4}> <MyComponent /> </Spitha>
This avoids unproductive re-renders from edge-case viewport events.
Spitha is built to support both raw HTML/CSS and framework-level styling systems like ChakraUI. All `style` and `children` props are passed through without mutation.
Spitha is architected as a composable performance optimization wrapper in React. It is implemented as a single top-level functional component that leverages internal React hooks to track visibility, throttle fetches, and memoize rendered subtrees. Each responsibility—render suppression, async deferral, style pass-through—is modularized for testability and extensibility.
const observerRef = useRef();
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting);
});
if (observerRef.current) observer.observe(observerRef.current);
return () => observer.disconnect();
}, []);
const fetchRef = useRef(false);
useEffect(() => {
if (isVisible && !fetchRef.current) {
fetchRef.current = true;
fetchData().then(data => {
setData(data);
fetchRef.current = false;
});
}
}, [isVisible]);
const stableCallback = useCallback(() => {
return fetchData();
}, [fetchData]);
const renderedOutput = useMemo(() => {
return <MyComponent data={data} />;
}, [data]);
useEffect(() => {
const onScroll = () => {
const rect = ref.current.getBoundingClientRect();
if (rect.top < window.innerHeight * threshold) {
setTriggerReady(true);
}
};
window.addEventListener("scroll", onScroll);
return () => window.removeEventListener("scroll", onScroll);
}, [threshold]);
The React reconciliation process compares virtual DOM trees to determine updates. In traditional React apps, prop changes—even non-functional ones—can trigger deep child re-renders. Spitha prevents this through:
useCallback
)fetchData()
to avoid layout thrashingAdditionally, render branches are internally profiled to ensure minimal time between component visibility and paint.
React’s rendering performance is often dictated not just by raw JSX complexity, but by when updates occur. Spitha defers updates until necessary, and ensures fetch logic happens during idle periods. This produces:
The launch page for Spitha is written in Next.js using ChakraUI. Spitha components are wrapped around hero images, feature sections, and testimonial tiles. Chakra's system for layout tokens and spacing props ensures style consistency while Spitha ensures update efficiency. This demonstrates real-world compatibility in a marketing-grade UI context.
Spitha is typed using advanced TypeScript generics to maintain full intellisense and prop inference. The fetch signature is defined as:
type SpithaProps = {
fetchData: () => Promise<any>;
initialData?: any[];
threshold?: number;
children?: React.ReactNode;
style?: React.CSSProperties;
}
This allows Spitha to be dropped into any React component tree without additional type configuration.
To support Chakra, Tailwind, and inline CSS, Spitha passes down all style
props and other DOM attributes untouched. This is done by destructuring and forwarding props to the child render tree:
return (
<div ref={observerRef} style={props.style}>
{props.children}
</div>
);
This ensures Spitha is transparent from a visual and developer standpoint—components wrapped by Spitha behave and appear exactly as they would otherwise.
Each instance of Spitha retains its own fetch state, observer registration, and memoization boundary. This localizes memory use and ensures GC eligibility when unmounted. React’s fiber architecture guarantees cleanup of all attached effects, keeping Spitha safe for heavy dynamic lists or route transitions.
Spitha’s performance was rigorously benchmarked using jest
within a controlled React environment. The tests compared:
To run the tests:
yarn jest
During the tests, rendering times were captured and output as markdown using this snippet:
const markdown = [
'# Rendering Times for Native Data of Normal Components vs Spitha',
'| Component | Time (milliseconds) |',
'|-----------|---------------------|'
];
renderingData.forEach((data) => {
markdown.push(`| ${data.normalRenderingTime} | ${data.myComponentRenderingTime} |`);
});
fs.writeFileSync('NEW_FILE_NAME.md', markdown.join('\n'));
The results demonstrated a consistent rendering performance improvement of approximately 40% across all benchmarked components.