<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: codewithkaushik</title>
    <description>The latest articles on DEV Community by codewithkaushik (@codewithkaushikk).</description>
    <link>https://dev.to/codewithkaushikk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1120050%2Ff65ef3ed-b383-4c5f-a0ef-47bfcf804bdb.png</url>
      <title>DEV Community: codewithkaushik</title>
      <link>https://dev.to/codewithkaushikk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codewithkaushikk"/>
    <language>en</language>
    <item>
      <title>Comprehensive guide to using Redux in React Native</title>
      <dc:creator>codewithkaushik</dc:creator>
      <pubDate>Sat, 15 Jul 2023 07:59:29 +0000</pubDate>
      <link>https://dev.to/codewithkaushikk/comprehensive-guide-to-using-redux-in-react-native-1knh</link>
      <guid>https://dev.to/codewithkaushikk/comprehensive-guide-to-using-redux-in-react-native-1knh</guid>
      <description>&lt;p&gt;React Native Redux Example | How To Use Redux In React Native is today’s leading topic. Redux is a standalone state management library, which can be used with any library or framework. If your background is React developer, then you have used the Redux library with React.&lt;br&gt;
&lt;strong&gt;Overview of React Native Redux Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Install React Native on mac.&lt;/li&gt;
&lt;li&gt;Step 2: Add TextBox and Button.&lt;/li&gt;
&lt;li&gt;Step 3: Define the state and input handler.&lt;/li&gt;
&lt;li&gt;Step 4: Create actions, reducers, and components folder.&lt;/li&gt;
&lt;li&gt;Step 5: Create a Reducer function.&lt;/li&gt;
&lt;li&gt;Step 6: Create a Redux store.&lt;/li&gt;
&lt;li&gt;Step 7: Pass the store to the React Native app.&lt;/li&gt;
&lt;li&gt;Step 8: Connect React Native app to the Redux store.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TCM_Dn7A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbeluierzhpu37kcbfi7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TCM_Dn7A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbeluierzhpu37kcbfi7.png" alt="Image description" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Native Redux Example Tutorial&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The primary use of &lt;strong&gt;Redux&lt;/strong&gt; is that we can use one application state as a global state and interact with the state from any react component is very easy, whether they are siblings or parent-child. Now, let us start the &lt;strong&gt;React Native&lt;/strong&gt; &lt;strong&gt;Redux Example Tutorial&lt;/strong&gt; by installing &lt;strong&gt;React Native on Mac&lt;/strong&gt; first.&lt;/p&gt;

&lt;p&gt;We start our project by installing &lt;strong&gt;React Native CLI&lt;/strong&gt; globally on the Mac. You can skip the following command if you have already installed it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Step 1: Install React Native.&lt;/strong&gt;&lt;br&gt;
Type the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g react-native-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, now for creating a new application, type the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;react-native@latest init AwesomeProject

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, after installing, we need to open this application in the two different &lt;strong&gt;Simulators&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For testing on &lt;strong&gt;iOS&lt;/strong&gt; simulator, type the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;react-native run-ios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have configured &lt;strong&gt;XCode&lt;/strong&gt; correctly, then one &lt;strong&gt;iOS&lt;/strong&gt; device will pop up as well as the development server will also start.&lt;/p&gt;

&lt;p&gt;To open the project inside the &lt;strong&gt;Android Simulator&lt;/strong&gt;, type the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;react-native run-android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the &lt;strong&gt;@reduxjs/toolkit&lt;/strong&gt; and &lt;strong&gt;react-redux&lt;/strong&gt; library using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @reduxjs/toolkit react-redux
  #or
yarn add @reduxjs/toolkit react-redux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#Step 2: Add Button into the App.js.&lt;/strong&gt;&lt;br&gt;
Okay, so we will add a text box and button to add places. So let us add the &lt;strong&gt;View&lt;/strong&gt; and &lt;strong&gt;Button&lt;/strong&gt;. Also, we will add the flexbox layout. Write the following code inside the **App.js **file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.js
import React from 'react';
import {Button,StyleSheet,View} from 'react-native';

const App = () =&amp;gt; {
  return (
    &amp;lt;View
      style={styles.mainContainer}
      &amp;lt;Button
        title={"Enable Dark Mode"}
      /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
};

mainContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'white',
  },

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#Step 3: Create the following folders inside Root.&lt;/strong&gt;&lt;br&gt;
Create the following folders.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;actions&lt;/li&gt;
&lt;li&gt;reducers&lt;/li&gt;
&lt;li&gt;components&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Inside the actions folder, create one file called &lt;strong&gt;types.js&lt;/strong&gt;. Add the following line inside &lt;strong&gt;types.js&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const ENABLE = 'ENABLE';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The action type is the reducer’s operation type. Based on the action type, the reducer case will be executed, and we modify the state in such a way that it remains pure. So we create a copy of the existing state and return a new state.&lt;/p&gt;

&lt;p&gt;Now, create one more file called &lt;strong&gt;theme.js&lt;/strong&gt; in the same directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// theme.js
import { ENABLE } from './types';
export const changeTheme = theme =&amp;gt; {
  return {
    type: ENABLE,
    payload: theme
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;changeTheme()&lt;/strong&gt; function returns an action. Now, based on that action, the reducers function’s case is executed.&lt;/p&gt;

&lt;p&gt;But, we need to connect this action to our App.js component somehow. Otherwise, we can not add the data to the Redux store. Also, we need first to create a store. But before, we also need to create a reducer function. So, first, create a reducer, then create a store and then connect the React Native application to the &lt;strong&gt;Redux&lt;/strong&gt; store.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Step 4: Create a Reducer function.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside &lt;strong&gt;reducers&lt;/strong&gt; function, create one file called &lt;strong&gt;themeReducer.js&lt;/strong&gt;. Add the following code inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//themeReducer.js
import { ENABLE } from '../actions/types';

const initialState = { isEnable: false };

export default function (state = initialState, action: any){
 const { type, payload } = action;
 switch (type) {
    case ENABLE:
      return {
        ...state,
        isLoggedIn: payload,
      };
    default:
      return state;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, here, we have defined the function called &lt;strong&gt;themeReducer&lt;/strong&gt; that accepts the two arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. state&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. action&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first time, it will take the &lt;strong&gt;initial state&lt;/strong&gt; of our application, and then we pass whatever argument; it takes that argument and operates based on the case execution. The second argument is &lt;strong&gt;action&lt;/strong&gt;, which consists of &lt;strong&gt;type&lt;/strong&gt; and &lt;strong&gt;payload&lt;/strong&gt;. The &lt;strong&gt;payload&lt;/strong&gt; is the place name; we have entered inside the text box. So it adds the text box’s value inside &lt;strong&gt;places&lt;/strong&gt; array.&lt;/p&gt;

&lt;p&gt;Remeber here; we have returned a new state and not existing state. So we have modified the state in a pure manner and not existing state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Step 5: Create a Combine Reducers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the reducer folder, create one file called index.js and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.js
import { combineReducers } from "redux";
import themeReducer from "./themeReducer";

export default combineReducers({
  themeReducer,
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 6: Create a Redux Store.
&lt;/h1&gt;

&lt;p&gt;Inside the root folder, create one file called &lt;strong&gt;store.js&lt;/strong&gt; and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//store.js
import { configureStore } from "@reduxjs/toolkit";
import { useDispatch } from "react-redux";
import {
  FLUSH,
  PAUSE,
  PERSIST,
  persistReducer,
  persistStore,
  PURGE,
  REGISTER,
  REHYDRATE,
} from "redux-persist";
import reduxStorage from "../Storage";
import rootReducer from "./reducer/index";

const persistConfig = {
  key: "root",
  version: 1,
  storage: reduxStorage,
  timeout: 0,
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =&amp;gt;
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
});

export const persistor = persistStore(store);
export const useAppDispatch = () =&amp;gt; useDispatch();
// export type RootState = ReturnType;

export default store;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 7: Pass the Store to the React Native app.
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
import { AppRegistry } from 'react-native';
import React from 'react';
import App from './App';
import { name as appName } from './app.json';
import { Provider } from 'react-redux';
import configureStore from './store';
const store = configureStore()
const RNRedux = () =&amp;gt; (
  &amp;lt;Provider store = { store }&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;
)
AppRegistry.registerComponent(appName, () =&amp;gt; RNRedux);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is almost the same as React web application, in which we pass the Provider as a root element and pass the store, and then via react-redux’s &lt;strong&gt;connect()&lt;/strong&gt; function, we can &lt;strong&gt;connect **the any react component to **redux&lt;/strong&gt; store.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Step 8: Connect React Native app to Redux store.&lt;/strong&gt;&lt;br&gt;
Finally, we connect our &lt;strong&gt;App.js&lt;/strong&gt; component to the &lt;strong&gt;Redux&lt;/strong&gt; store. For that, we need the &lt;strong&gt;connect()&lt;/strong&gt; function from the &lt;strong&gt;react-redux&lt;/strong&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//app.js
import React from 'react';
import { Button,StyleSheet,View } from 'react-native';
import {useSelector} from 'react-redux';
import {useAppDispatch} from './App/Store/store';
import {changeTheme} from './App/actions/theme.js';

const App = () =&amp;gt; {
  const theme = useSelector(state =&amp;gt; state.theme);
  const dispatch = useAppDispatch();
  const [isEnabled, setIsEnabled] = React.useState(theme.darkTheme);
  const toggleSwitch = () =&amp;gt; setIsEnabled(!isEnabled);

  React.useEffect(() =&amp;gt; {
    if (isEnabled) {
      dispatch(changeTheme({isEnable: true}));
    }
    if (!isEnabled) {
      dispatch(changeTheme({isEnable: false}));
      console.log(isEnabled);
    }
  }, [isEnabled]);

  return (
    &amp;lt;View
      style={[
        styles.mainContainer,
        {backgroundColor: isEnabled ? 'black' : 'white'},
      ]}&amp;gt;
      &amp;lt;Button
        title={isEnabled ? 'Enable Light Mode' :  "Enable Dark Mode"}
        onPress={() =&amp;gt; {
          toggleSwitch();
        }}
      /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
};

const styles = StyleSheet.create({
  mainContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'white',
  },
});

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done...&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>redux</category>
      <category>reduxdarktheme</category>
    </item>
  </channel>
</rss>
