The user wants a boilerplate for a React Native app using RTK and TypeScript, which likely means I should provide a folder structure with key files like package.json, tsconfig, and others. It’s important to be concise, avoiding verbosity, and I should consider whether to target the bare React Native CLI or Expo since the user didn’t specify. I need a structure with main files that's consistent with versions around late 2024, considering React Native 0.76, TypeScript 5.6, and Redux Toolkit2.2.Creating a minimal RN boilerplate
I can provide several key files for the user’s RN boilerplate, including package.json, tsconfig.json, babel.config.js, and index.js or index.ts as the entry file. I should also include crucial components like App.tsx, the store at src/app/store.ts, and the counter feature files. I’ll mention that they can create the project with npx react-native init and then replace the default files. It’s important to clarify TS integration in RN, highlighting that RN supports TS out-of-the-box. I'll keep the instructions concise and straightforward.Structuring RN boilerplate files
I need to provide a clear folder structure for the RN boilerplate, which includes files like package.json, tsconfig.json, and entries like index.ts and App.tsx. A possible optional file is src/app/rootReducer.ts, and I’ll include the counter feature files too. I think it’s best to offer just the relevant parts of package.json, as a long file might confuse the user. Instead of full JSON, I'll simply list key dependencies and suggest they run npm install for missing parts.Providing installation and config details
I’ll include commands to install the required libraries, followed by the relevant configuration files. For typed hooks, I want to show how to create the types for RootState and AppDispatch, along with the custom hooks for dispatch and selector. I’ll provide the setup for store.ts, showing how to configure the Redux store with middleware.
Then, I’ll display App.tsx, including the Provider and CounterScreen component. I'll also share how CounterScreen utilizes the hooks and define the counter slice with the initial state and reducers.Creating config files and instructions
For the tsconfig file, I’ll provide the necessary compiler options, including settings for ESNext and module resolution. I’ll also include paths for easy import aliases. Next, I’ll show the Babel configuration, which can start simple, maybe just using the default preset. While mentioning babel-plugin-module-resolver, I can keep that as an optional step to simplify things.
Then, I’ll present index.ts to properly register the main app component. I’ll offer a brief instruction on creating the app via CLI using npx react-native init MyApp --template react-native-template-typescript. I'll ensure the content matches the user's request for a boilerplate by outlining steps for dependencies, folder structure, config files, and core Redux files in concise one-liners. Here’s a minimal but complete boilerplate for a React Native app using TypeScript and Redux Toolkit.
1. Create the base project
Use the official TypeScript template:
bash
npx react-native init MyApp --template react-native-template-typescript
cd MyApp
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
If you already have index.js, you can rename it to index.ts and adjust.
// src/app/store.ts
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
6. Typed Redux hooks (src/app/hooks.ts)
ts
// src/app/hooks.ts
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from './store';
// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
7. Example slice (src/features/counter/counterSlice.ts)