Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usecontext multiple provider

import React, { useContext, useState } from "react";
const CountContext = React.createContext("count");

const DescendantA = () => {
  const { count, setCount, count2, setCount2 } = useContext(CountContext);

  return (
    <>
      <button onClick={() => setCount((c) => c + 1)}>Click me {count}</button>
      <button onClick={() => setCount2((c) => c + 1)}>Click me {count2}</button>
    </>
  );
};
const DescendantB = () => {
  const { count, setCount, count2, setCount2 } = useContext(CountContext);

return (
    <>
      <button onClick={() => setCount((c) => c + 1)}>Click me {count}</button>
      <button onClick={() => setCount2((c) => c + 1)}>Click me {count2}</button>
    </>
  );
};
export default function App() {
  const [count, setCount] = useState(0);
  const [count2, setCount2] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount, count2, setCount2 }}>
      <DescendantA />
      <DescendantB />
    </CountContext.Provider>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: color switcher 
Javascript :: babel core cdn 
Javascript :: how to add prefix to a string in javascript 
Javascript :: search query in javascript 
Javascript :: react native file pdf to base64 
Javascript :: port y build - vite.config.js 
Javascript :: get latest input by .each jquery 
Javascript :: The DOM Parent-Child Relationship 
Javascript :: js spin wheel color 
Javascript :: Finding palindrome using for loop 
Javascript :: javascript Adding Element to the Inner Array 
Javascript :: javascript Arguments Binding 
Javascript :: javascript for...of with Sets 
Javascript :: javascript Using yield to Pause Execution 
Javascript :: socket io join multiple rooms 
Javascript :: React ES6 Modules 
Javascript :: how to check if a number is divisible by 3 and 5 in javascript 
Javascript :: switch javascript to java 
Javascript :: change origin xy phaser 
Javascript :: phaser random rectangle 
Javascript :: phaser animation random delay 
Javascript :: data tables ajust columns after init 
Javascript :: check letter case 
Javascript :: get product 
Javascript :: Get index of child elements with event listener in JavaScript 
Javascript :: iterating map javascript 
Javascript :: javascript case insensitive regex 
Javascript :: for in loop 
Javascript :: object set js 
Javascript :: how to make a delete button in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =