23
loading...
This website collects cookies to deliver better user experience
const React, { useState } from 'react';
const IncreaseButton = ({count, setCount}) => {
return (
<button
onClick={() => setCount(count + 1)}
>
Increase value to {count + 1}
</button>
)
}
const DecreaseButton = ({count, setCount}) => {
return (
<button
onClick={() => setCount(count - 1)}
>
Decrease value to {count - 1}
</button>
)
}
const Clicker = ({count, setCount}) => {
return (
<div>
<IncreaseButton count={count} setCount={setCount}/>
<DecreaseButton count={count} setCount={setCount}/>
</div>
)
}
const App = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>{count}</p>
<Clicker count={count} setCount={setCount} />
</div>
);
}
Clicker
which renders two buttons with their own logic which uses the count
and setCount
variables. You can also see that Clicker
isn't doing anything with the count
and setCount
data, but it still has to be passed through to reach the components that need it.import {createContext} from 'react';
const MyContext = createContext();
import {createContext} from 'react';
interface IMyContext {
...
}
const MyContext = createContext<IMyContext>();
// MyContext.jsx
import React, {createContext, useState} from 'react';
export const MyContext = createContext({
count: 0,
setCount: () => {},
});
export const MyContextProvider = ({children}) => {
const [count, setCount] = useState(0);
return (
<MyContext.Provider value={{count, setCount}}>
{children}
</MyContext.Provider>
)
}
value
which could be any type of data if you're only using 1 piece of data in your context. For more than one item in your context, you need to pass it as an object which should match the stuff in your context. For instance, in the example above, the MyContext
object is expecting a count and a setCount key. The value
in the provider has an object with the same count and setCount keys being set.App.jsx
(topmost) file.import React from 'react';
import { MyContextProvider } from './MyContext';
const App = ({ children }) => {
return (
<MyContextProvider>
{children}
</MyContextProvider>
);
}
import React from 'react';
import { MyContext } from './MyContext';
const MyComponentWithContext = () => {
return (
<MyContext.Consumer>
{(context) => (
<p>The current count is {context.count}</p>
)}
</MyContext.Consumer>
);
}
import React from 'react';
import { MyContext } from './MyContext';
const MyComponentWithContext = () => {
return (
<MyContext.Consumer>
{({count}) => ( <p>The current count is {count}</p> )} </MyContext.Consumer>
);
}
useContext
hook, you still need to make sure you're inside the provider, but you don't have to mess around with consumers anymore. Like the consumer, you can use destructuring with the useContext
hook to only get the variables you need. If I refactor the component from above to use the useContext
hook, it looks like this:import React, { useContext } from 'react';
import { MyContext } from './MyContext';
const MyComponentWithContext = () => {
const {count} = useContext(MyContext);
return (
<p>The current count is {context.count}</p>
);
}
import React from 'react';
import { MyFirstContextProvider } from './MyFirstContext';
import { MySecondContextProvider } from './MySecondContext';
import { MyThirdContextProvider } from './MyThirdContext';
const App = ({ children }) => {
return (
<MyFirstContextProvider>
<MySecondContextProvider>
<MyThirdContextProvider>
{children}
</MyThirdContextProvider>
</MySecondContextProvider>
</MyFirstContextProvider>
);
}
MySecondContext
, then you need to be sure and use that specific context when creating the consumer or calling the useContext
hook.