Skip to content
TheChatArchiveThe Chat Archive
GeminiGemini 3 Pro Preview· googleHugging Face Frontier Reasoning Traces

Generate a boilerplate for a React Native app with Redux Toolkit and TypeScript.

  1. User

    Generate a boilerplate for a React Native app with Redux Toolkit and TypeScript.

  2. Gemini Assistant

    Comprehending the Foundation

    I'm now grasping the user's objective: a React Native boilerplate incorporating Redux Toolkit and TypeScript. I've begun pinpointing essential components like the React Native framework itself. The React Native CLI is the standard, and I'm considering initial setup steps.

    Analyzing the Core Elements

    I'm now delving into the essential components for the boilerplate, solidifying the need for React Navigation. I have identified the need to account for a modular approach to the folder structure, as this is crucial for the boilerplate's scalability. I have also begun the process of identifying key files and their contents, as well as the necessary setup steps.

    Outlining the Structure

    I'm presently building the essential step-by-step instructions. I've broken it down to project initialization, dependency installations, and folder structure specification. The Redux setup, including store configuration, typed hooks, and a sample slice is next. Also, React Navigation's stack navigator setup is underway, along with the creation of a sample screen component.

    Developing the Code Snippets

    I'm now generating code snippets for the Redux store, including typed hooks and an example slice. I'm focusing on the React Navigation stack navigator setup, and designing a basic home screen component for a complete boilerplate. I'm keeping everything clean and concise.

    Planning the Structure

    Now, I'm integrating the latest React Native initialization specifics, focusing on current commands. I'm prioritizing the essential folder structure, with a focus on maintainability. I'm beginning the Redux store setup and the setup for typed hooks, aiming for clear and concise code. The next step will be working on the example slice and then the navigation setup.

    Here is a complete, modular boilerplate for a React Native app using TypeScript, Redux Toolkit, and React Navigation.

    1. Initialization & Installation

    First, create a new React Native project (TypeScript is now the default in modern React Native versions):

    npx @react-native-community/cli init MyApp
    cd MyApp
    

    Next, install the necessary dependencies:

    # Redux Toolkit & React-Redux
    npm install @reduxjs/toolkit react-redux
    
    # React Navigation (Core + Stack)
    npm install @react-navigation/native @react-navigation/native-stack
    
    # Navigation Peer Dependencies
    npm install react-native-screens react-native-safe-area-context
    

    (For iOS, don't forget to run cd ios && pod install)


    2. Project Structure

    Create a src folder to keep the root clean. Recommended structure:

    MyApp/
    ├── src/
    │   ├── app/
    │   │   ├── store.ts       # Store configuration
    │   │   └── hooks.ts       # Typed Redux hooks
    │   ├── features/
    │   │   └── counter/       # Feature-based folder (Slice + Components)
    │   │       └── counterSlice.ts
    │   ├── navigation/
    │   │   └── AppNavigator.tsx
    │   ├── screens/
    │   │   └── HomeScreen.tsx
    │   └── components/
    ├── App.tsx
    └── index.js
    

    3. Redux Setup

    Create the Slice (src/features/counter/counterSlice.ts)

    This manages a specific part of your state.

    import { createSlice, PayloadAction } from '@reduxjs/toolkit';
    import { RootState } from '../../app/store';
    
    interface CounterState {
      value: number;
    }
    
    const initialState: CounterState = {
      value: 0,
    };
    
    export const counterSlice = createSlice({
      name: 'counter',
      initialState,
      reducers: {
        increment: (state) => {
          // Redux Toolkit allows us to write "mutating" logic in reducers.
          state.value += 1;
        },
        decrement: (state) => {
          state.value -= 1;
        },
        incrementByAmount: (state, action: PayloadAction<number>) => {
          state.value += action.payload;
        },
      },
    });
    
    export const { increment, decrement, incrementByAmount } = counterSlice.actions;
    
    // Selector
    export const selectCount = (state: RootState) => state.counter.value;
    
    export default counterSlice.reducer;
    

    Configure the Store (src/app/store.ts)

    Combine your slices here.

    import { configureStore } from '@reduxjs/toolkit';
    import counterReducer from '../features/counter/counterSlice';
    
    export const store = configureStore({
      reducer: {
        counter: counterReducer,
        // add other reducers here
      },
    });
    
    // Infer the `RootState` and `AppDispatch` types from the store itself
    export type RootState = ReturnType<typeof store.getState>;
    export type AppDispatch = typeof store.dispatch;
    

    Typed Hooks (src/app/hooks.ts)

    Use these hooks throughout your app instead of plain useDispatch and useSelector.

    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 = () => useDispatch<AppDispatch>();
    export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
    

    4. Components & Screens

    Home Screen (src/screens/HomeScreen.tsx)

    An example screen that interacts with the Redux store.

    import React from 'react';
    import { View, Text, Button, StyleSheet } from 'react-native';
    import { useAppSelector, useAppDispatch } from '../app/hooks';
    import { increment, decrement, selectCount } from '../features/counter/counterSlice';
    
    const HomeScreen = () => {
      const count = useAppSelector(selectCount);
      const dispatch = useAppDispatch();
    
      return (
        <View style={styles.container}>
          <Text style={styles.title}>Redux Toolkit Boilerplate</Text>
          <Text style={styles.counter}>Count: {count}</Text>
          
          <View style={styles.buttonGroup}>
            <Button title="Decrement" onPress={() => dispatch(decrement())} />
            <View style={{ width: 20 }} />
            <Button title="Increment" onPress={() => dispatch(increment())} />
          </View>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      },
      title: {
        fontSize: 24,
        fontWeight: 'bold',
        marginBottom: 20,
      },
      counter: {
        fontSize: 48,
        marginBottom: 30,
      },
      buttonGroup: {
        flexDirection: 'row',
      },
    });
    
    export default HomeScreen;
    

    5. Navigation Setup

    App Navigator (src/navigation/AppNavigator.tsx)

    import React from 'react';
    import { NavigationContainer } from '@react-navigation/native';
    import { createNativeStackNavigator } from '@react-navigation/native-stack';
    import HomeScreen from '../screens/HomeScreen';
    
    // Define type definitions for routes
    export type RootStackParamList = {
      Home: undefined;
      // Details: { id: string }; // Example of route with params
    };
    
    const Stack = createNativeStackNavigator<RootStackParamList>();
    
    const AppNavigator = () => {
      return (
        <NavigationContainer>
          <Stack.Navigator initialRouteName="Home">
            <Stack.Screen name="Home" component={HomeScreen} />
          </Stack.Navigator>
        </NavigationContainer>
      );
    };
    
    export default AppNavigator;
    

    6. Main Entry Point

    Update App.tsx

    Wrap the application with the Redux Provider.

    import React from 'react';
    import { Provider } from 'react-redux';
    import { store } from './src/app/store';
    import AppNavigator from './src/navigation/AppNavigator';
    
    const App = () => {
      return (
        <Provider store={store}>
          <AppNavigator />
        </Provider>
      );
    };
    
    export default App;
    

    Summary of Logic Flow

    1. App.tsx initializes the Redux Provider and NavigationContainer.
    2. AppNavigator renders the HomeScreen.
    3. HomeScreen uses useAppSelector to read data from the store and useAppDispatch to trigger actions.
    4. counterSlice handles the logic (reducers) and updates the store.
    5. The UI automatically re-renders because it subscribes to the store changes.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.