Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Created November 13, 2020 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpaulhunt/8de3be748c434383ef7df6e5c31a1dc8 to your computer and use it in GitHub Desktop.
Save davidpaulhunt/8de3be748c434383ef7df6e5c31a1dc8 to your computer and use it in GitHub Desktop.
React Context Example 1
import * as React from "react";
const Ctx = React.createContext({
currency: "USD",
format: (val) => val,
setCurrency: () => {},
});
Ctx.displayName = "CurrencyCtx";
function CurrencyProvider(props) {
const [currency, setCurrency] = React.useState("USD");
const format = React.useCallback(
(value) => {
return `${value} ${currency}`;
},
[currency]
);
return (
<Ctx.Provider value={{ currency, format, setCurrency }} {...props} />
);
}
const useCurrency = () => React.useContext(Ctx);
export { CurrencyProvider, useCurrency };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment