zahid ansari
Building scalable systems by applying mental models to code and architecture.
Website
Install this app to stay connected and in the loop.
Building meaningful experiences one pixel at a time 😉.
Made with by Zahid
views
zahid ansari
Building scalable systems by applying mental models to code and architecture.
Website
Install this app to stay connected and in the loop.
Building meaningful experiences one pixel at a time 😉.
Made with by Zahid
views

Share
Don't write the same code twice. Strategies for sharing hooks, types, and utility functions between your web and mobile apps.
Maintaining two separate codebases for Web (Next.js) and Mobile (React Native) is painful when they share the same business logic.
I had a Next.js website and an Expo app. Every time I updated the user validation logic or the API client in one, I had to copy-paste it to the other. Mistakes happened, and bugs appeared in one platform but not the other.
Even without a complex Monorepo tool like Nx or Turborepo, you can structure your logic to be platform-agnostic.
This hook works perfectly in both Next.js and React Native because it relies on standard React hooks and standard fetch/Axios, not DOM elements.
1// hooks/useUser.ts
2import { useState, useEffect } from 'react';
3import { apiClient } from '../api/client';
4
5export const useUser = (userId: string) => {
6 const [user, setUser] = useState(null);
7 const [loading, setLoading] = useState(true);
8
9 useEffect(() => {
10 apiClient.get(`/users/${userId}`)
11 .then(res => setUser(res.data))
12 .finally(() => setLoading(false));
13 }, [userId]);
14
15 return { user, loading };
16};1// shared/schema.ts
2import { z } from 'zod';
3
4// This runs anywhere JS runs
5export const loginSchema = z.object({
6 email: z.string().email(),
7 password: z.string().min(8),
8});1<div>1ViewStart small by sharing types and constants. Eventually, move your intricate business logic hooks into a shared folder. Your future self will thank you for the DRY code.
Tech Stack: Next.js, React Native, TypeScript