Lädt...


🔧 How to Use the React Context API in Your Projects


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Managing the state is an essential part of developing applications in React. A common way to manage the state is by passing props. Passing props means sending data from one component to another. It's a good way to ensure data gets to the right place in a React application.

1. Create a New React App

npx create-react-app contextapi

2. Create the Context

Create an App.js file in your project and createContext in this App.js as well we have put some data like name, price, info and image in the component.

  • Setting up state using useState.
  • Providing context to child components.

App.js

import React, { useState } from "react";
import Home from "./Home";

import { createContext } from "react";

export const UserContext = createContext();
function App() {
  const [stuName, setStuName] = useState({
    name: "Bowie Alderney Sofa",
    price: "INR : 25,000/-",
    info: "One of the many things that make a house a home is a 
     beautiful, comfortable sofa or armchair.",
    image:
      "./image.jpg",
  });

  return (
    <>
      <UserContext.Provider value={{ stuName, setStuName }}>
        <Home />
      </UserContext.Provider>
    </>
  );
}

export default App;

3. Create the Home.jsx Component.
if you want to access data then we need to use useContext, First we have import usecontextand use it.

  • Displaying product details.
  • Using context to access state.

Home.jsx

import React from "react";

import { useContext } from "react";
import { UserContext } from "./App";

import Products from "./Products";
import Cart from "./Cart";

function Home() {
  const users = useContext(UserContext);
  return (
    <div>
      <div className="container">
        <div>
          <div class="card mt-5" style={{ width: "18rem" }}>
            <img
              class="card-img-top"
              src={users.stuName.image}
              alt="Card image cap"
            />
            <div class="card-body">
              <h4 class="card-title">{users.stuName.name}</h4>
              <h3 class="card-title">{users.stuName.price}</h3>
              <p class="card-text">{users.stuName.info}</p>
              <a href="#" class="btn btn-primary">
                <Cart />
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default Home;

4.Creating the Cart Component

  • Using useContext to consume context.
  • Updating state with a button click.
import React from "react";
import { useContext } from "react";
import { UserContext } from "./App";
function Cart() {
  const users = useContext(UserContext);
  return (
    <>
      <button
        class="btn btn-primary"
        onClick={() => {
          users.setStuName({
            name: "Sofas",
            price: "INR : 55,000/-",
            info: "One of the many things that make a house a home is a 
            beautiful, comfortable sofa or armchair.",
            image:
              "./image02.jpg",
          });
        }}
      >
        Update Cart
      </button>
    </>
  );
}

export default Cart;

5. Run the Application
Finally, start the React development server.

npm start

Output:

Image description

...

🔧 How to Use the React Context API in Your Projects


📈 42.58 Punkte
🔧 Programmierung

🔧 How to Use the React Context API in Your Projects


📈 42.58 Punkte
🔧 Programmierung

🔧 How to use the Context API, What is Context API?


📈 40.63 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 39.5 Punkte
🔧 Programmierung

🔧 Redux-Toolkit vs React Context API: Mastering State Management in React


📈 32.16 Punkte
🔧 Programmierung

🔧 How to Use React Suspense to Improve your React Projects


📈 31.55 Punkte
🔧 Programmierung

🔧 State Management in React: When to Use Context API vs. Redux


📈 29.6 Punkte
🔧 Programmierung

🔧 How to use React Context API with npm library


📈 29.6 Punkte
🔧 Programmierung

🔧 How to Use React's Context API – Tutorial with Examples


📈 29.6 Punkte
🔧 Programmierung

🔧 Context API vs. Redux: When and Why to Use Them in React


📈 29.6 Punkte
🔧 Programmierung

🔧 Understanding The Use Of Context API In React JS


📈 29.6 Punkte
🔧 Programmierung

🔧 React & TypeScript: How to use Context API and useReducer with Firestore Database?


📈 29.6 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 28.18 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 28.18 Punkte
🔧 Programmierung

🔧 How to Use React Context in Your Project – Beginner's Guide


📈 27.07 Punkte
🔧 Programmierung

🔧 Understanding State Management in React: Differences Between Redux, Context API, and Recoil


📈 25.12 Punkte
🔧 Programmierung

🔧 State Management in React Native: Redux, Context API, MobX, and Zustand


📈 25.12 Punkte
🔧 Programmierung

🔧 Understanding State Management in React: Differences Between Redux, Context API, and Recoil


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context API


📈 25.12 Punkte
🔧 Programmierung

🔧 Redux-Toolkit vs React Context API: A Deep Dive into State Management.💪🚀🚀


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context-API Pro | Build state management using useContext + useReducer | Typescript


📈 25.12 Punkte
🔧 Programmierung

🔧 Effective State Management in React: Comparing Redux, Context API, and Recoil


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context API Explained with Examples


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context APi


📈 25.12 Punkte
🔧 Programmierung

🔧 React | Context API vs Zustand


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context API: A Comprehensive Guide


📈 25.12 Punkte
🔧 Programmierung

🔧 React Context API: A Hilarious Journey into State Management 🚀


📈 25.12 Punkte
🔧 Programmierung

🔧 HANDLING AUTH IN REACT APPS USING NANOSTORES AND CONTEXT API


📈 25.12 Punkte
🔧 Programmierung

🔧 Context API in React Explained


📈 25.12 Punkte
🔧 Programmierung

🔧 Stop abuse React Context API


📈 25.12 Punkte
🔧 Programmierung

🔧 How to Manage State in React Apps with APIs – Redux, Context API, and Recoil Examples


📈 25.12 Punkte
🔧 Programmierung

🔧 🚀 Day 13: Context API in React 🚀


📈 25.12 Punkte
🔧 Programmierung

🔧 State Management in React –Props vs the Context API


📈 25.12 Punkte
🔧 Programmierung

🔧 Simplify State Management with React.js Context API: Full Guide


📈 25.12 Punkte
🔧 Programmierung

matomo