I see that the user wants boilerplate code for a React Native app using Redux Toolkit and TypeScript. They probably expect a file structure along with code templates. I should consider including a basic counter slice, store, and typed hooks in my example, while keeping it minimal and not adding navigation unless necessary.
I need to think about what constitutes the "boilerplate." This includes things like package.json, tsconfig.json, and key files like App.tsx and index.js. I'll provide concise but comprehensive code within proper code fences, ensuring not to exceed the allowances.Clarifying boilerplate requirements
I need to focus on the latest patterns for my boilerplate, like using configureStore, createSlice, and Provider from react-redux. I also want to demonstrate how to create typed RootState and AppDispatch.
Now, regarding React Native CLI versus Expo: if the user hasn't specified, I’ll lean toward bare RN, since that's often preferred, even though Expo is popular too. They asked for "boilerplate," so my response will be independent of the CLI. I’ll provide a minimal file tree and corresponding code snippets without going into too much detail on running the app. Keeping it concise is key!Creating package.json and tsconfig
I’m working on the package.json for the React Native app. I should set the name, version, and private status, while including a scripts section and dependencies like React and React Native. Since I can't guarantee the latest version, I might keep it generic or suggest using a wildcard, though the user may just copy it directly.
My knowledge cutoff is October 2024, and by 2025, React Native might be on version 0.76. So, I want to keep the versioning flexible like ">=0.74.0 <1.0.0." For Redux Toolkit and React-Redux, I’ll point to the latest major versions. The tsconfig will reflect the typical configuration for React Native as well.Drafting App.tsx and related files
In my App.tsx, I’ll import the Provider from 'react-redux' and set up the store. The Counter component will come from the counter slice, which I’ll create in counterSlice.ts using createSlice from Redux Toolkit. I'll define a state object with a number value, plus actions for incrementing, decrementing, and incrementing by a specified amount.
In store.ts, I’ll configure the store with the counter reducer. I need to create typed hooks in hooks.ts for dispatch and selector functionality.
In the App component, I’ll use the selector to display the counter value and dispatch actions with buttons from 'react-native'. For index.js, I'll include AppRegistry and wrap the app in the component, ensuring I have Babel set up with the right presets. Since the user just wants boilerplate code, I'll give them everything as a single response containing all necessary code.Outlining file structure and configuration
I’m laying out the file structure for the React Native app, which will include essential files like package.json, tsconfig.json, and the src directory containing store and hooks. The App component will be in App.tsx.
For tsconfig, I need to set "jsx" to "react-native" because I’m using React 18. I’ll base my config on the typical RN template for TypeScript, ensuring it’s minimal while retaining necessary compiler options.
I’ll also ensure to import type definitions for RootState and AppDispatch correctly and handle the circular import situation. In index.js, I’ll set up AppRegistry as usual, which will work well with the TypeScript in App.tsx. I’ll segment the code into manageable chunks to keep it clear and concise when I respond. Here’s a minimal boilerplate for a React Native app using Redux Toolkit and TypeScript.
It includes:
Basic project structure
Redux store setup with Redux Toolkit
Typed hooks (useAppDispatch, useAppSelector)
A simple counter slice and screen wired into the app
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
7. src/app/store.ts – Redux store setup
ts
import {configureStore} from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
// Inferred types for use throughout the app
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
8. src/app/hooks.ts – Typed Redux 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;
9. src/features/counter/counterSlice.ts – Example slice
This is a ready-to-use boilerplate layout.
You can drop these files into a fresh React Native TypeScript project (e.g. from npx react-native init YourApp --template react-native-template-typescript) and then adjust dependencies/versions as needed.
If you want, I can also give you a version tailored to Expo or add navigation (e.g. React Navigation) on top of this.