Git Product home page Git Product logo

sofiane-abou-abderrahim / react-elegant-context Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.12 MB

๐Ÿš€ Elevate your React apps with advanced state management techniques! Explore component composition, Context API for seamless state sharing, and Reducers for managing complex states. Check out "React Elegant Context" demo now! ๐Ÿ’ปโœจ

Home Page: https://sofiane-abou-abderrahim.github.io/react-elegant-context/

HTML 2.76% JavaScript 67.23% CSS 30.01%
prop-drilling react react-context react-context-api react-reducer reactjs component-composition context-api-react javascript reducers state-management

react-elegant-context's Introduction

Advanced State Management

Beyond Basic Apps & "Lifting Up State"

  • The Problem of Shared State: Prop Drilling
  • Embracing Component Composition
  • Sharing State with Context
  • Managing Complex State with Reducers

Steps

0. Understanding Prop Drilling & Project Overview

  1. run npm install
  2. run npm run dev
  3. create README.md

1. Prop Drilling: Component Composition as a Solution

  1. embrace component composition by refactoring the Shop.jsx component to a wrapper around the list of products
  2. output the <Product /> component inside the <Shop> component in App.jsx

2. Creating & Providing The Context

  1. create a store folder
  2. create a shopping-cart-context.jsx file
  3. inside of this file, create a CartContext context
  4. provide it to this application and wrap it around the components in App.jsx by using <CartContext.Provider>

3. Consuming the Context

  1. consume the CartContext in the Cart.jsx component
    1. import { CartContext }
    2. import { useContext }
    3. set useContext(CartContext) as a value to cartCtx
    4. use cartCtx to access the items property
  2. add a value to <CartContext.Provider> in App.jsx
  3. destructure CartContext so that you use { items } straight away

4. Linking the Context to State

  1. set the value of <CartContext.Provider> to the shoppingCart state in App.jsx to link the context to state
  2. to edit the state with context, create a ctxValue const which will be an object
  3. use CartContext in Product.jsx
  4. add addItemToCart function as an initial value in shopping-cart-context.jsx file

5. A Different Way Of Consuming Context

  1. use the <CartContext.Consumer> component to wrap the JSX code in Cart.jsx
  2. pass a function as a child inside <CartContext.Consumer> to automatically receive the consumed cartCtx value as a parameter

6. Migrating the Entire Demo Project to use the Context API

  1. remove extra props
  2. use CartContext in Header.jsx
  3. add the updateItemQuantity function in the ctxValue constant in App.jsx & in shopping-cart-context.jsx
  4. extract updateItemQuantity in Cart.jsx & call it on the buttons

7. Outsourcing Context & State Into a Separate Provider Component

  1. export a CartContextProvider function in shopping-cart-context.jsx
  2. import CartContextProvider & use it in App.jsx

8. Introducing the useReducer Hook

  1. import useReducer in shopping-cart-context.jsx
  2. use useReducer() hook to manage the state

9. Dispatching Actions & Editing State with useReducer

  1. dispatch actions with useReducer() hook in shopping-cart-context.jsx
  2. edit the shoppingCartReducer function with the actions values

react-elegant-context's People

Contributors

sofiane-abou-abderrahim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.