<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Anany on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Anany on Medium]]></description>
        <link>https://medium.com/@ahmedenany9812?source=rss-f74f26d1b67c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RvhwkxiSTrRUJSbMUpmZ-Q.jpeg</url>
            <title>Stories by Ahmed Anany on Medium</title>
            <link>https://medium.com/@ahmedenany9812?source=rss-f74f26d1b67c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 10:54:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmedenany9812/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[State Management in Next JS 13 App Router]]></title>
            <link>https://medium.com/@ahmedenany9812/state-management-in-next-js-13-app-router-3892a56261ce?source=rss-f74f26d1b67c------2</link>
            <guid isPermaLink="false">https://medium.com/p/3892a56261ce</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Ahmed Anany]]></dc:creator>
            <pubDate>Tue, 20 Jun 2023 13:18:20 GMT</pubDate>
            <atom:updated>2023-06-20T13:18:20.731Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_iYn7jVvNO3Njt-XDgPiuQ.png" /></figure><p>Next.js, a JavaScript framework developed by Vercel, has gained immense popularity for its ability to seamlessly combine the best of both React and server-side rendering (SSR). By integrating server-side rendering with React’s component-based architecture, Next.js offers developers a powerful toolset to build fast, interactive, and SEO-friendly web applications. Furthermore, Next.js 13 embraces the latest advancements in the React ecosystem, providing seamless integration with React 18. You can take advantage of React 18’s new features, such as Concurrent Mode and Server Components, to unlock even greater performance gains and create highly responsive user interfaces.</p><p>In conclusion, Next.js 13 is a game-changer in the world of web development. With its unrivaled combination of server-side rendering, React integration, enhanced performance, and developer-friendly features, Next.js 13 empowers you to build exceptional web applications with ease. Whether you’re a seasoned developer or just starting your coding journey, Next.js 13 is here to revolutionize your web development experience and unlock limitless possibilities. Get ready to elevate your projects to new heights with Next.js 13!</p><p>State management plays a crucial role in developing complex web applications with seamless user experiences. While Next.js itself doesn’t provide a built-in state management solution, it seamlessly integrates with various popular state management libraries and patterns, allowing developers to choose the one that best fits their project’s requirements. Let’s explore some commonly used state management options in Next.js 13</p><blockquote>Remember, the choice of state management solution ultimately depends on your project’s complexity, scalability needs, and personal preference. By leveraging the power of Next.js 13 and the diverse ecosystem of state management libraries, you can effectively manage state in your Next.js applications and deliver exceptional user experiences.</blockquote><h3><strong>1- Redux toolkit</strong></h3><p>Redux is a widely adopted state management library that offers a predictable state container for JavaScript applications.</p><p>With Next.js 13, integrating Redux Toolkit into your Next.js applications for state management has never been easier. Redux Toolkit is a powerful library that simplifies the process of managing state in Redux by providing a set of utilities, including simplified syntax, predefined middleware, and a built-in immutable update pattern. Let’s explore how you can seamlessly use Redux Toolkit in Next.js 13:</p><p>With Next.js 13, integrating Redux Toolkit into your Next.js applications for state management has never been easier. Redux Toolkit is a powerful library that simplifies the process of managing state in Redux by providing a set of utilities, including simplified syntax, predefined middleware, and a built-in immutable update pattern. Let’s explore how you can seamlessly use Redux Toolkit in Next.js 13:</p><p><strong>1.Installation</strong></p><p>Start by installing the required dependencies. In your Next.js project directory, run the following command:</p><pre>npm install @reduxjs/toolkit react-redux</pre><p><strong>2.Setting up the Redux Store<br></strong> in your Next.js project’s App directory. Create a file, such as Store.js`, to define your Redux store configuration.</p><pre>import { configureStore } from &#39;@reduxjs/toolkit&#39;;<br> // Import your reducers<br> import Reducer from &#39;./Slices&#39;;<br>const store = configureStore({<br> reducer: {<br> Reducer,<br> // Add more reducers as needed<br> },<br> });<br>export default store;</pre><p>In the code snippet above, we import `configureStore` from `<a href="http://twitter.com/reduxjs/toolkit">@reduxjs/toolkit</a>` and define our Redux store with the desired reducers. Make sure to create separate reducer files (e.g., `counter.js`) for each slice of state in your application.</p><p><strong>3.Creating Reducers<br></strong>Create a separate file for each reducer. For example, in Slice.js`, you would define your slice of state and associated actions using the Redux Toolkit `createSlice` function:</p><pre>import { createSlice } from &#39;@reduxjs/toolkit&#39;;<br>const counterSlice = createSlice({<br> name: &#39;counter&#39;,<br> initialState: 0,<br> reducers: {<br> increment: (state) =&gt; state + 1,<br> decrement: (state) =&gt; state - 1,<br> },<br> });<br>export const { increment, decrement } = counterSlice.actions;<br>export default counterSlice.reducer;</pre><p>In this example, we define a counter slice of state with an initial value of 0 and two actions: `increment` and `decrement`.</p><p><strong>4.Connecting Redux to Next.js</strong><br> In your Next.js application, you can connect the Redux store to your components using the `Provider` component from `react-redux`.</p><p>In the layout.js` file inside the app directory import the `Provider` component and wrap your application with it:</p><pre>&quot;use client&quot;// Because we&#39;re inside a server component<br><br>import { Provider } from &#39;react-redux&#39;;<br>import store from &#39;../store&#39;;<br>export default function RootLayout({ children }) {<br>return (<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;body&gt;<br>&lt;Provider store={store}&gt;<br>   {children}<br>&lt;/Provider&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>);<br>}</pre><p>By wrapping your application with the `Provider` component and providing the Redux store, all components within your Next.js application can access the Redux store and dispatch actions.</p><p>You can also keep layout.js a server component by Adding an External Provider then Exporting it inside layout.js</p><p>ReduxProvider.js</p><pre>&quot;use client&quot;;<br><br>import { Provider } from &quot;react-redux&quot;;<br>import Store from &quot;./Store&quot;;<br><br>export default function ReduxProvider({ children }) {<br>  return &lt;Provider store={Store}&gt;{children}&lt;/Provider&gt;;<br>}</pre><p>layout.js</p><pre>import ReduxProvider from &quot;@/Lib/ReduxProvider&quot;;<br>import &quot;./globals.css&quot;;<br>import { Inter } from &quot;next/font/google&quot;<br>const inter = Inter({ subsets: [&quot;latin&quot;] });<br>export const metadata = {<br>  title: &quot;&quot;,<br>  description: &quot;&quot;,<br>  icons: {<br>    icon: &quot;/favicon.ico&quot;,<br>  },<br>};<br><br>export default function RootLayout({ children }) {<br>  return (<br>    &lt;html lang=&quot;en&quot;&gt;<br>      &lt;body className={inter.className}&gt;<br>        &lt;ReduxProvider&gt;{children}&lt;/ReduxProvider&gt;<br>      &lt;/body&gt;<br>    &lt;/html&gt;<br>  );<br>}</pre><p><strong>5. Accessing State and Dispatching Actions</strong><br> In any component that needs access to the Redux state or needs to dispatch actions, you can use the `useSelector` and `useDispatch` hooks from `react-redux`.</p><pre>&quot;use client&quot;// Because we&#39;re inside a server component<br>import { useSelector, useDispatch } from ‘react-redux’;<br>import { increment, decrement } from ‘../store/counter’;<br>function CounterComponent() {<br> const counter = useSelector((state) =&gt; state.counter);<br> const dispatch = useDispatch();<br>return (<br> &lt;div&gt;<br> &lt;p&gt;Counter: {counter}&lt;/p&gt;<br> &lt;button onClick={() =&gt; dispatch(increment())}&gt;Increment&lt;/button&gt;<br> &lt;button onClick={() =&gt;<br>dispatch(decrement())}&gt;Decrement&lt;/button&gt;<br> &lt;/div&gt;<br> );<br> }</pre><p>In the example above, we use `useSelector` to access the counter state from the Redux store and `useDispatch` to dispatch the `increment` and `decrement` actions.</p><h3><strong>2-React Context Hook</strong></h3><p>Using React Context with Next.js allows you to share state or data across components without the need for prop drilling. Next.js seamlessly integrates with React Context, making it a powerful tool for managing global state or sharing data across your Next.js application. Let’s explore how you can utilize React Context in Next.js:</p><p><strong>1.Create a Context</strong><br> In your Next.js project, you can create a new file, such as `AppContext.js`, to define your React Context.</p><pre>&quot;use client&quot;// Because we&#39;re inside a server component<br>import React, { createContext, useState, useContext } from &#39;react&#39;;<br>// Create the context<br> const AppContext = createContext();<br>// Create a provider component<br> export const AppProvider = ({ children }) =&gt; {<br> const [state, setState] = useState(initialState);<br>// Define any functions or values you want to provide<br> const value = {<br> state,<br> setState,<br> };<br>return &lt;AppContext.Provider value={value}&gt;{children}&lt;/AppContext.Provider&gt;;<br> };<br>// Export the context<br> export const useAppContext=()=&gt; useContext(AppContext)</pre><p>In this example, we create the `AppContext` using `createContext()`. We also create a provider component, `AppProvider`, which wraps the child components and provides the state and setter functions through the context.</p><p><strong>2.Set Up the Provider in </strong><strong>layout.js`</strong></p><pre>import {AppProvider} from &quot;@/Lib/AppContext&quot;;<br>import &quot;./globals.css&quot;;<br>import { Inter } from &quot;next/font/google&quot;<br>const inter = Inter({ subsets: [&quot;latin&quot;] });<br>export const metadata = {<br>  title: &quot;&quot;,<br>  description: &quot;&quot;,<br>  icons: {<br>    icon: &quot;/favicon.ico&quot;,<br>  },<br>};<br><br>export default function RootLayout({ children }) {<br>  return (<br>    &lt;html lang=&quot;en&quot;&gt;<br>      &lt;body className={inter.className}&gt;<br>        &lt;AppProvider&gt;{children}&lt;/AppProvider&gt;<br>      &lt;/body&gt;<br>    &lt;/html&gt;<br>  );<br>}</pre><p>By wrapping your Next.js application with `AppProvider`, the state and setter functions will be accessible to all components nested within the provider.</p><p><strong>3.Accessing Context in Components<br></strong> To access the context and its state or functions, you can use the `useContext` hook in your components.</p><pre>&quot;use client&quot;<br><br>import {useAppContext}&#39;../AppContext&#39;;<br>function MyComponent() {<br> const { state, setState } = useAppContext()<br>// Use the state and setState as needed<br>return &lt;div&gt;{state}&lt;/div&gt;;<br> }</pre><p>In this example, we import `AppContext` from `AppContext.js` and use `useContext` to access the state and setter functions provided by the context. You can use the state and setter functions within the component as needed.</p><h3><strong>3-Zustand</strong></h3><p>Zustand is a lightweight state management library for React applications. It provides a simple and intuitive API for managing state in your components, allowing you to easily create, update, and share state across your application. With its minimalistic approach and focus on performance, Zustand offers a powerful alternative to more complex state management solutions.</p><p>Integrating Zustand with Next.js 13 is a seamless process that allows you to leverage Zustand’s lightweight state management capabilities within your Next.js applications. You can follow these steps to utilize Zustand in Next.js 13</p><p><strong>1.Installation<br></strong> Start by installing Zustand and its required dependencies. In your Next.js project directory, run the following command:</p><pre>npm install zustand</pre><p><strong>2.Creating Zustand Store:<br></strong> In your Next.js project, create a file, such as `store.js`, to define your Zustand store.</p><pre>import { create } from &#39;zustand&#39;;<br>const useStore = create((set) =&gt; ({<br> counter: 0,<br> increment: () =&gt; set((state) =&gt; ({ counter: state.counter + 1 })),<br> decrement: () =&gt; set((state) =&gt; ({ counter: state.counter - 1 })),<br> }));<br>export default useStore;</pre><p>In this example, we use the `create` function from Zustand to define our store. The store contains a `counter` state and two functions, `increment` and `decrement`, to update the counter value.</p><p><strong>3.Accessing Zustand Store in Components:<br></strong> In any component where you need access to the Zustand store, you can use the `useStore` hook.</p><pre>&quot;use client&quot;<br><br>import useStore from &#39;../store&#39;;<br>function MyComponent() {<br> const counter = useStore((state) =&gt; state.counter);<br> const increment = useStore((state) =&gt; state.increment);<br> const decrement = useStore((state) =&gt; state.decrement);<br>return (<br> &lt;div&gt;<br> &lt;p&gt;Counter: {counter}&lt;/p&gt;<br> &lt;button onClick={increment}&gt;Increment&lt;/button&gt;<br> &lt;button onClick={decrement}&gt;Decrement&lt;/button&gt;<br> &lt;/div&gt;<br> );<br> }</pre><p>In this example, we import `useStore` from `store.js` and use it to access the Zustand store. By passing a selector function to `useStore`, we extract the required state or actions from the store.</p><p><strong>4.Using Zustand Store Provider in </strong><strong>layout.js`:<br></strong> To ensure the Zustand store is available throughout your Next.js application, you can use the Zustand store provider in the layout.js` file.</p><pre>&quot;use client&quot;// Because we&#39;re inside a server component<br><br>import { Provider } from &#39;zustand&#39;;<br>import useStore from &#39;../store&#39;;<br>export default function RootLayout({ children }) {<br>return (<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;body&gt;<br>&lt;Provider createStore={useStore}&gt;<br> {children}<br>&lt;/Provider&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>);<br>}</pre><p>You can also keep layout.js a server component by Adding an External Provider then Exporting it inside layout.js</p><p>ZustandProvider.js</p><pre>&quot;use client&quot;;<br>import { Provider } from &#39;zustand&#39;;<br>import useStore from &#39;../store&#39;;<br>export default function ZustandProvider({ children }) {<br>  return &lt;Provider createStore={useStore}&gt;{children}&lt;/Provider&gt;;<br>}</pre><p>layout.js</p><pre>import ZustandProvider from &quot;@/Lib/ZustandProvider&quot;;<br>import &quot;./globals.css&quot;;<br>import { Inter } from &quot;next/font/google&quot;<br>const inter = Inter({ subsets: [&quot;latin&quot;] });<br>export const metadata = {<br>  title: &quot;&quot;,<br>  description: &quot;&quot;,<br>  icons: {<br>    icon: &quot;/favicon.ico&quot;,<br>  },<br>};<br>export default function RootLayout({ children }) {<br>  return (<br>    &lt;html lang=&quot;en&quot;&gt;<br>      &lt;body className={inter.className}&gt;<br>        &lt;ZustandProvider&gt;{children}&lt;/ZustandProvider&gt;<br>      &lt;/body&gt;<br>    &lt;/html&gt;<br>  );<br>}</pre><p>Whether you choose React Context, Redux Toolkit, Zustand, or any other state management approach, Next.js 13 allows you to seamlessly integrate and leverage the chosen solution within your application. This empowers you to efficiently manage state, handle complex interactions, and deliver exceptional user experiences.</p><p>Remember to consider your project’s requirements, complexity, scalability needs, and personal preferences when selecting a state management solution. With the power of Next.js 13 and the diverse ecosystem of state management libraries available, you have the flexibility to choose the approach that best suits your specific needs.</p><p>By adopting a robust state management solution in Next.js 13, you can ensure clean and maintainable code, improved performance, and a streamlined development experience, ultimately resulting in high-quality web applications that provide seamless user interactions.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3892a56261ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>