30
loading...
This website collects cookies to deliver better user experience
import { createContext } from 'react';
export const CountContext = createContext();
import { useState } from 'react';
import CountContext from './contexts/CountContext';
const App = () => {
const [count, setCount] = useState();
return (
<CountContext.Provider value={{ count, setCount }}>
<ChildComponent />
</ CountContext.Provider>
)
}
import { useContext } from 'react';
import CountContext from './contexts/CountContext';
const ChildComponent = () => {
const { count, setCount } = useContext(CountContext);
return (
<button onClick={setCount((previousValue) => previousValue + 1)}></button>
<p>Count is: {count}</p>
)
}