SlideShare ist ein Scribd-Unternehmen logo
1 von 49
REACT CONTEXT
React Meetup
18.08.21
JIRACHAI URAIJAREE (HUAD)
Full-stack Developer
INTRODUCTION & APIs
DIVE DEEPER & TIPS
EXAMPLES
INTRODUCTION & APIs
DIVE DEEPER & TIPS
EXAMPLES
In a typical React application, data is passed top-down (parent to child) via props, but such usage can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are
required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree.
When to Use Context Before You Use Context API React.createContext Context.Provider Class.contextType Context.Consumer Context.displayName Examples Dynamic Context Updating Context
from a Nested Component Consuming Multiple Contexts Caveats Legacy API When to Use Context Context is designed to share data that can be considered “global” for a tree of React components,
such as the current authenticated user, theme, or preferred language. For example, in the code below we manually thread through a “theme” prop in order to style the Button component:
Etiam massa nunc, ultricies ac velit nec, dignissim ultrices turpis. Etiam vel sodales justo. Phasellus scelerisque id ante ac fermentum. Curabitur ut vulputate nisl. Vestibulum et lorem
erat. Phasellus at tempus lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Fusce rutrum blandit tortor, at congue odio suscipit eget. Nunc felis orci,
fermentum id neque sit amet, tempor volutpat eros.
Before You Use Context Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it makes component
reuse more difficult. If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context.
Aliquam eu dapibus sapien. Maecenas eu felis id nibh pellentesque ultrices sed at risus. Vivamus blandit, massa quis tempus convallis, arcu est vehicula sapien, vitae tristique purus
metus sed dui. Curabitur ultricies libero varius accumsan mollis. Nullam venenatis magna lobortis, ornare nisl quis, mollis ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus.
Praesent quis quam et tortor commodo tincidunt.
Creates a Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the
tree. The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This default value can be helpful for testing components in isolation
without wrapping them. Note: passing undefined as a Provider value does not cause consuming components to use defaultValue.
Proin non dictum odio. Maecenas mattis augue eu mi pulvinar blandit. Integer iaculis non lacus vitae efficitur. In commodo, nisi ut rutrum rhoncus, massa sapien tempus nulla, et
tincidunt nulla dolor a justo. Aenean euismod metus a varius bibendum. Suspendisse urna sem, auctor in cursus ac, pharetra ut elit. Praesent eget dignissim purus. Nulla eu neque pharetra, facilisis
eros a, luctus turpis. Nullam venenatis, arcu nec condimentum ullamcorper, diam ante efficitur dolor, vitae accumsan nibh purus eget massa. Sed dignissim auctor viverra. Suspendisse placerat
vehicula tortor, ut laoreet urna condimentum interdum. Proin libero diam, eleifend sed felis a, bibendum convallis nibh.
Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes. The Provider component accepts a value prop to be
passed to consuming components that are descendants of this Provider. One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree. All
consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes. The propagation from Provider to its descendant consumers (including .contextType and
useContext) is not subject to the shouldComponentUpdate method, so the consumer is updated even when an ancestor component skips an update. Changes are determined by comparing the
new and old values using the same algorithm as Object.is.
A React component that subscribes to context changes. Using this component lets you subscribe to a context within a function component. Requires a function as a child. The function
receives the current context value and returns a React node. The value argument passed to the function will be equal to the value prop of the closest Provider for this context above in the tree. If
there is no Provider for this context above, the value argument will be equal to the defaultValue that was passed to createContext().
Because context uses reference identity to determine when to re-render, there are some gotchas that could trigger unintentional renders in consumers when a provider’s parent re-
renders. For example, the code below will re-render all consumers every time the Provider re-renders because a new object is always created for value:
INTRODUCTION & APIs
What’s Context?
“Context provides a way to pass data through the component tree
without having to pass props down manually at every level.”
- https://reactjs.org/docs/context.html
“ Another way allowing you to implicitly share data (state) between components ”
-
me
INTRODUCTION & APIs
What’s Context?
INTRODUCTION & APIs
State sharing by passing props
const Parent = () => {
const state = 1;
return (
<>
<Child state={state} />
<Child state={state} />
<Child state={state} />
</>
);
};
Props Drilling 𑁋 Passing prop(s) down multiple levels
INTRODUCTION & APIs
Props Drilling
const GrandParent = () => <Parent state={state} />;
const Parent = () => <Child state={state} />;
const Child = () => <GrandChild state={state} />;
const GrandChild = () => <p>{state}</p>;
INTRODUCTION & APIs
Props Drilling - The problem
Unnecessary re-renders in component tree
Flashing border == Component re-renders
INTRODUCTION & APIs
Creating Context instance
import { createContext } from "react";
const ColorContext = createContext(undefined);
INTRODUCTION & APIs
Context Provider
const MainApp = () => {
const [currentColor, setCurrentColor] = useState("");
return (
<ColorContext.Provider value={{ currentColor, setCurrentColor }}>
<SetColorButtons />
<GrandParent />
</ColorContext.Provider>
);
};
INTRODUCTION & APIs
Consuming Context
import { useContext } from "react";
const SetColorButtons = () => {
const colorContextValue = useContext(ColorContext);
return (
...buttons with onClick={() => colorContextValue.setCurrentShape(color)}
);
};
INTRODUCTION & APIs
Consuming Context
import { useContext } from "react";
const Color = () => {
const colorContextValue = useContext(ColorContext);
return (
<div style={{ background: colorContextValue.currentColor }}>
Color: {color}
</div>
);
};
INTRODUCTION & APIs
Passing data with Context
[Consumer] useContext(ColorContext)
[Provider] <ColorContext.Provider>
INTRODUCTION & APIs
DIVE DEEPER & TIPS
EXAMPLES
DIVE DEEPER & TIPS
Consuming Context Outside Provider
const MainApp = () => {
const [currentColor, setCurrentColor] = useState("");
return (
<>
<GrandParent />
<ColorContext.Provider value={{ currentColor, setCurrentColor }}>
<SetColorButtons />
<GrandParent />
</ColorContext.Provider>
</>
);
};
DIVE DEEPER & TIPS
Default Error Message
DIVE DEEPER & TIPS
Consuming Context Outside Provider
const Color = () => {
const colorContextValue = useContext(ColorContext);
return (
<div style={{ background: colorContextValue.currentColor }}>
Color: {color}
</div>
);
};
const ColorContext = createContext(undefined);
DIVE DEEPER & TIPS
Consuming Context Outside Provider
const MainApp = () => {
const [currentColor, setCurrentColor] = useState("");
return (
<>
<GrandParent />
<ColorContext.Provider value={{ currentColor, setCurrentColor }}>
<SetColorButtons />
<GrandParent />
</ColorContext.Provider>
</>
);
};
DIVE DEEPER & TIPS
Custom Error Message
const Color = ({ color }) => {
const colorContextValue = useContext(ColorContext);
if (!colorContextValue) {
throw new Error("ColorContext must be used within ColorProvider");
}
return (
<div style={{ background: colorContextValue.currentColor }}>
Color: {color}
</div>
);
};
DIVE DEEPER & TIPS
Abstraction
const ColorProvider = ({ children }) => {
const [currentColor, setCurrentColor] = useState("");
return (
<ColorContext.Provider
value={{ currentColor, setCurrentColor }}
>
{children}
</ColorContext.Provider>
);
};
const MainApp = () => {
return (
<ColorProvider>
<SetColorButtons />
<GrandParent />
</ColorProvider>
);
};
DIVE DEEPER & TIPS
Abstraction
const useColor = () => {
const colorContextValue = useContext(ColorContext);
if (!colorContextValue) {
throw new Error("useColor must be used within ColorProvider");
}
return useContext(ColorContext);
};
const Color = () => {
const colorContextValue = useColor();
return (
<div style={{ background: colorContextValue.currentColor }}>
Color: {color}
</div>
);
};
DIVE DEEPER & TIPS
Simple drawing app!
DIVE DEEPER & TIPS
Global state
const PaintContext = createContext(undefined);
const PaintProvider = ({ children }) => {
const [currentShape, setCurrentShape] = useState("");
const [currentColor, setCurrentColor] = useState("");
const value = {
currentShape,
currentColor,
setCurrentShape,
setCurrentColor,
};
return <PaintContext.Provider value={value}>{children}</PaintContext.Provider>;
};
const usePaint = () => {
const paintContextValue = useContext(PaintContext);
if (!paintContextValue) {
throw new Error("usePaint must be used within PaintProvider");
}
return paintContextValue;
};
DIVE DEEPER & TIPS
Provider at root level
const App = () => {
return (
<PaintProvider>
<Header />
<MainLayout>
<LeftPanel />
<RightPanel />
</MainLayout>
</PaintProvider>
);
};
const PaintPreview = () => {
const { currentShape, currentColor } = usePaint();
return <RenderedObject shape={currentShape} color={currentColor} />;
};
const ShapePreview = () => {
const { currentShape } = usePaint();
return <RenderedObject shape={currentShape} />;
};
const ColorPreview = () => {
const { currentColor } = usePaint();
return <div>{currentColor}</div>;
};
const ShapeSelection = () => {
const { setCurrentShape } = usePaint();
return ...buttons with onClick={() => setCurrentShape(shape)}
};
const ColorSelection = () => {
const { setCurrentColor } = usePaint();
return ...buttons with onClick={() => setCurrentShape(color)}
};
DIVE DEEPER & TIPS
Global state at root level
[Provider] <PaintContext.Provider>
[Consumer]
const { currentShape,currentColor} = usePaint();
[Consumer]
const { currentShape } = usePaint();
[Consumer]
const { currentColor } = usePaint();
[Consumer]
const { setCurrentShape } = usePaint();
[Consumer]
const { setCurrentColor } = usePaint();
DIVE DEEPER & TIPS
Move Provider closer to where it’s needed
const App = () => {
return (
<>
<Header />
<PaintProvider>
<MainLayout>
<LeftPanel />
<RightPanel />
</MainLayout>
</PaintProvider>
</>
);
};
ShapePreview & ShapeSelection does NOT need
to re-render when color changed
DIVE DEEPER & TIPS
Problem with Global State
... Vice Versa
DIVE DEEPER & TIPS
Breaking global state into smaller related states
const PaintContext = createContext(undefined);
const PaintProvider = ({ children }) => {
const [currentShape, setCurrentShape] = useState("");
const [currentColor, setCurrentColor] = useState("");
const value = {
currentShape,
currentColor,
setCurrentShape,
setCurrentColor,
};
return <PaintContext.Provider value={value}>{children}</PaintContext.Provider>;
};
const usePaint = () => {
const paintContextValue = useContext(PaintContext);
if (!paintContextValue) {
throw new Error("usePaint must be used within PaintProvider");
}
return paintContextValue;
};
DIVE DEEPER & TIPS
Break into 2 Contexts
const ShapeContext = createContext(undefined);
const ShapeProvider = ({ children }) => {
const [currentShape, setCurrentShape] = useState("");
const value = { currentShape, setCurrentShape };
return <ShapeContext.Provider value={value}>{children}</ShapeContext.Provider>;
};
const useShape = () => {
const shapeContextValue = useContext(ShapeContext);
if (!shapeContextValue)... throw error ...
return shapeContextValue;
};
const ColorContext = createContext(undefined);
const ColorProvider = ({ children }) => {
const [currentColor, setCurrentColor] = useState("");
const value = { currentColor, setCurrentColor };
return <ColorContext.Provider value={value}>{children}</ColorContext.Provider>;
};
const useColor = () => {
const colorContextValue = useContext(ColorContext);
if (!colorContextValue)... throw error ...
return colorContextValue;
};
DIVE DEEPER & TIPS
Abstraction layer (Optional)
const PaintProvider = ({ children }) => {
return (
<ShapeProvider>
<ColorProvider>{children}</ColorProvider>
</ShapeProvider>
);
};
DIVE DEEPER & TIPS
Consuming Context separately
const PaintPreview = () => {
const { currentShape } = useShape();
const { currentShape } = useColor();
return <RenderedObject shape={currentShape} color={currentColor} />;
};
const ShapePreview = () => {
const { currentShape } = useShape();
return <RenderedObject shape={currentShape} />;
};
const ColorPreview = () => {
const { currentColor } = useColor();
return <div>{currentColor}</div>;
};
const ShapeSelection = () => {
const { setCurrentShape } = useShape();
return ...buttons with onClick={() => setCurrentShape(shape)}
};
const ColorSelection = () => {
const { setCurrentColor } = useColor();
return ...buttons with onClick={() => setCurrentShape(color)}
};
DIVE DEEPER & TIPS
Consuming Context separately
[Provider] <PaintContext.Provider>
[Consumer]
const { currentShape } = useShape();
const { currentColor } = useColor();
[Consumer]
const { currentShape } = useShape();
[Consumer]
const { setCurrentShape } = useShape();
[Consumer]
const { currentColor } = useColor();
[Consumer]
const { setCurrentColor } = useColor();
DIVE DEEPER & TIPS
Unnecessary re-render
ShapeSelection has no reason to re-render
…Same for ColorSelection
DIVE DEEPER & TIPS
Prevent re-render with React.memo
import { memo } from "react";
const ShapeSelection = memo(() => {
const { setCurrentShape } = useShape();
return ...buttons with onClick={() => setCurrentShape(shape)}
});
DIVE DEEPER & TIPS
React.memo does not stop component from re-rendering
DIVE DEEPER & TIPS
Prevent re-render with React.memo
import { memo } from "react";
const ShapeSelection = memo(() => {
const { setCurrentShape } = useShape();
return ...buttons with onClick={() => setCurrentShape(shape)}
});
❌
DIVE DEEPER & TIPS
Unnecessary re-render
DIVE DEEPER & TIPS
Consuming Context
const PaintPreview = () => {
const { currentShape } = useShape();
const { currentShape } = useColor();
return <RenderedObject shape={currentShape} color={currentColor} />;
};
const ShapePreview = () => {
const { currentShape } = useShape();
return <RenderedObject shape={currentShape} />;
};
const ColorPreview = () => {
const { currentColor } = useColor();
return <div>{currentColor}</div>;
};
const ShapeSelection = () => {
const { setCurrentShape } = useShape();
return ...buttons with onClick={() => setCurrentShape(shape)}
};
const ColorSelection = () => {
const { setCurrentColor } = useColor();
return ...buttons with onClick={() => setCurrentShape(color)}
};
DIVE DEEPER & TIPS
Split Context
const ShapeProvider = ({ children }) => {
const [currentShape, setCurrentShape] = useState("");
const value = { currentShape, setCurrentShape };
return <ShapeContext.Provider value={value}>{children}</ShapeContext.Provider>;
};
DIVE DEEPER & TIPS
Split Context
const ShapeGetterContext = createContext(undefined);
const ShapeSetterContext = createContext(undefined);
const ShapeProvider = ({ children }) => {
const [currentShape, setCurrentShape] = useState("");
const getterValue = useMemo(() => ({ currentShape }), [currentShape]);
const setterValue = useMemo(() => ({ setCurrentShape }), []);
return (
<ShapeGetterContext.Provider value={getterValue}>
<ShapeSetterContext.Provider value={setterValue}>{children}</ShapeSetterContext.Provider>
</ShapeGetterContext.Provider>
);
};
const useShapeGetter = () => {
const shapeGetterContextValue = useContext(ShapeGetterContext);
if (!shapeGetterContextValue) ... throw error ...
return shapeGetterContextValue;
};
const useShapeSetter = () => {
const shapeSetterContextValue = useContext(ShapeSetterContext);
if (!shapeSetterContextValue) ... throw error ...
return shapeSetterContextValue;
};
DIVE DEEPER & TIPS
Split Context
DIVE DEEPER & TIPS
Context Display Name in DevTool
ShapeGetterContext.displayName = "ShapeGetterContext";
ShapeSetterContext.displayName = "ShapeSetterContext";
ColorGetterContext.displayName = "ColorGetterContext";
ColorSetterContext.displayName = "ColorSetterContext";
INTRODUCTION & APIs
DIVE DEEPER & TIPS
EXAMPLES
EXAMPLES
Use cases
● Global State
○ ThemeProvider
○ LocaleProvider
● Compound Component [Flexible & Implicitly share state]
○ CollapsiblePanel
EXAMPLES
Use cases
const PanelGetterContext = createContext(undefined);
const PanelSetterContext = createContext(undefined);
PanelGetterContext.displayName = "PanelGetterContext";
PanelSetterContext.displayName = "PanelSetterContext";
const usePanelState = () => {
const context = useContext(PanelGetterContext);
if (context === undefined) throw new Error("...");
return context;
};
const usePanelStateToggle = () => {
const context = useContext(PanelSetterContext);
if (!context) throw new Error("...");
return context;
};
const PanelLabel = ({ children }) => {
const toggle = usePanelStateToggle();
return <div onClick={toggle}>{children}</div>;
};
const PanelDetails = ({ children }) => {
const open = usePanelState();
return open ? <div>{children}</div> : null;
};
const Panel = ({ children }) => {
const [open, setOpen] = useState(false);
const toggle = useCallback(() => setOpen((o) => !o), []);
return (
<PanelGetterContext.Provider value={open}>
<PanelSetterContext.Provider value={toggle}>{children}</PanelSetterContext.Provider>
</PanelGetterContext.Provider>
);
};
<Panel>
<label>Collapsible Panel</label>
<PanelLabel>
<h1>Label</h1>
</PanelLabel>
<PanelDetails>
<h2>Details...</h2>
<p>Details...</p>
</PanelDetails>
</Panel>
INTRODUCTION & APIs
Why… When... What...
DIVE DEEPER & TIPS
Do’s… Dont’s… Optimization...
EXAMPLES
Global State… Collapsible Panel...
Q&A
Thank you for listening
Credits
https://reactjs.org/docs/context.html
https://giphy.com/explore/confused-travolta
https://knowyourmeme.com/memes/feels-good

Weitere ähnliche Inhalte

Was ist angesagt?

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming PrinciplesAndrii Lundiak
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to ReactRob Quick
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to HooksSoluto
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.jsEmanuele DelBono
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 
React Class Components vs Functional Components: Which is Better?
React Class Components vs Functional Components: Which is Better?React Class Components vs Functional Components: Which is Better?
React Class Components vs Functional Components: Which is Better?Fibonalabs
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
React for Dummies
React for DummiesReact for Dummies
React for DummiesMitch Chen
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSKnoldus Inc.
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_jsMicroPyramid .
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsANKUSH CHAVAN
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMJimit Shah
 

Was ist angesagt? (20)

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
React and redux
React and reduxReact and redux
React and redux
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
React Class Components vs Functional Components: Which is Better?
React Class Components vs Functional Components: Which is Better?React Class Components vs Functional Components: Which is Better?
React Class Components vs Functional Components: Which is Better?
 
routing.pptx
routing.pptxrouting.pptx
routing.pptx
 
React hooks
React hooksReact hooks
React hooks
 
React hooks
React hooksReact hooks
React hooks
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
 

Ähnlich wie React context

what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptxBOSC Tech Labs
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with ReactNetcetera
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Katy Slemon
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxBOSC Tech Labs
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 
How to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptxHow to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptxBOSC Tech Labs
 
Murach : How to work with session state and cookies
Murach : How to work with session state and cookiesMurach : How to work with session state and cookies
Murach : How to work with session state and cookiesMahmoudOHassouna
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥Remo Jansen
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionVisual Engineering
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)Chiew Carol
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksKaty Slemon
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...Edureka!
 
Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)Clarence Ngoh
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 

Ähnlich wie React context (20)

what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptx
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with React
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptx
 
Intro react js
Intro react jsIntro react js
Intro react js
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
How to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptxHow to use UseContext Hook in React.pptx
How to use UseContext Hook in React.pptx
 
Murach : How to work with session state and cookies
Murach : How to work with session state and cookiesMurach : How to work with session state and cookies
Murach : How to work with session state and cookies
 
Lecture14
Lecture14Lecture14
Lecture14
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Jdbc
JdbcJdbc
Jdbc
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooks
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
 
Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)
 
SharePoint Framework y React
SharePoint Framework y ReactSharePoint Framework y React
SharePoint Framework y React
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
ReactJs
ReactJsReactJs
ReactJs
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 

Mehr von Seven Peaks Speaks

Seven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made EasySeven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made EasySeven Peaks Speaks
 
Seven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose AnimationSeven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose AnimationSeven Peaks Speaks
 
Seven Peaks Speaks - Compose Navigation
Seven Peaks Speaks - Compose NavigationSeven Peaks Speaks - Compose Navigation
Seven Peaks Speaks - Compose NavigationSeven Peaks Speaks
 
How to Get Better Performance Out of Your App
How to Get Better Performance Out of Your AppHow to Get Better Performance Out of Your App
How to Get Better Performance Out of Your AppSeven Peaks Speaks
 
Secure Development of Azure Function
Secure Development of Azure FunctionSecure Development of Azure Function
Secure Development of Azure FunctionSeven Peaks Speaks
 
Develop Security & Compliances in Azure
Develop Security & Compliances in AzureDevelop Security & Compliances in Azure
Develop Security & Compliances in AzureSeven Peaks Speaks
 
Background Processing With Work Manager
Background Processing With Work ManagerBackground Processing With Work Manager
Background Processing With Work ManagerSeven Peaks Speaks
 
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Seven Peaks Speaks
 
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Seven Peaks Speaks
 
Delivering react app with confidence: Testing Pyramid
Delivering react app with confidence: Testing PyramidDelivering react app with confidence: Testing Pyramid
Delivering react app with confidence: Testing PyramidSeven Peaks Speaks
 
Getting hooked on performance and clean code
Getting hooked on performance and clean codeGetting hooked on performance and clean code
Getting hooked on performance and clean codeSeven Peaks Speaks
 
Establishing secure Biometric authentication in Android
Establishing secure Biometric authentication in AndroidEstablishing secure Biometric authentication in Android
Establishing secure Biometric authentication in AndroidSeven Peaks Speaks
 
Utilizing kotlin flows in an android application
Utilizing kotlin flows in an android applicationUtilizing kotlin flows in an android application
Utilizing kotlin flows in an android applicationSeven Peaks Speaks
 
Continuously deploy a containerized app to “Azure App Service”
Continuously deploy a containerized app to “Azure App Service”Continuously deploy a containerized app to “Azure App Service”
Continuously deploy a containerized app to “Azure App Service”Seven Peaks Speaks
 

Mehr von Seven Peaks Speaks (20)

BKK Web: Working with SEO
BKK Web: Working with SEOBKK Web: Working with SEO
BKK Web: Working with SEO
 
Seven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made EasySeven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made Easy
 
Seven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose AnimationSeven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose Animation
 
Seven Peaks Speaks - Compose Navigation
Seven Peaks Speaks - Compose NavigationSeven Peaks Speaks - Compose Navigation
Seven Peaks Speaks - Compose Navigation
 
How to Get Better Performance Out of Your App
How to Get Better Performance Out of Your AppHow to Get Better Performance Out of Your App
How to Get Better Performance Out of Your App
 
RxSubject And Operators
RxSubject And OperatorsRxSubject And Operators
RxSubject And Operators
 
Concurrency in Swift
Concurrency in SwiftConcurrency in Swift
Concurrency in Swift
 
DevSecOps on Azure
DevSecOps on AzureDevSecOps on Azure
DevSecOps on Azure
 
Secure Development of Azure Function
Secure Development of Azure FunctionSecure Development of Azure Function
Secure Development of Azure Function
 
Develop Security & Compliances in Azure
Develop Security & Compliances in AzureDevelop Security & Compliances in Azure
Develop Security & Compliances in Azure
 
Effective Lists Management
Effective Lists ManagementEffective Lists Management
Effective Lists Management
 
Layout Preview Tooling
Layout Preview ToolingLayout Preview Tooling
Layout Preview Tooling
 
Background Processing With Work Manager
Background Processing With Work ManagerBackground Processing With Work Manager
Background Processing With Work Manager
 
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Graph ql vs rest api - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
 
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
Structuring node.js projects - Seven Peaks Software (Node.JS Meetup 18 nov 2021)
 
Delivering react app with confidence: Testing Pyramid
Delivering react app with confidence: Testing PyramidDelivering react app with confidence: Testing Pyramid
Delivering react app with confidence: Testing Pyramid
 
Getting hooked on performance and clean code
Getting hooked on performance and clean codeGetting hooked on performance and clean code
Getting hooked on performance and clean code
 
Establishing secure Biometric authentication in Android
Establishing secure Biometric authentication in AndroidEstablishing secure Biometric authentication in Android
Establishing secure Biometric authentication in Android
 
Utilizing kotlin flows in an android application
Utilizing kotlin flows in an android applicationUtilizing kotlin flows in an android application
Utilizing kotlin flows in an android application
 
Continuously deploy a containerized app to “Azure App Service”
Continuously deploy a containerized app to “Azure App Service”Continuously deploy a containerized app to “Azure App Service”
Continuously deploy a containerized app to “Azure App Service”
 

Kürzlich hochgeladen

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

React context

  • 3. INTRODUCTION & APIs DIVE DEEPER & TIPS EXAMPLES
  • 4. INTRODUCTION & APIs DIVE DEEPER & TIPS EXAMPLES
  • 5. In a typical React application, data is passed top-down (parent to child) via props, but such usage can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree. When to Use Context Before You Use Context API React.createContext Context.Provider Class.contextType Context.Consumer Context.displayName Examples Dynamic Context Updating Context from a Nested Component Consuming Multiple Contexts Caveats Legacy API When to Use Context Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language. For example, in the code below we manually thread through a “theme” prop in order to style the Button component: Etiam massa nunc, ultricies ac velit nec, dignissim ultrices turpis. Etiam vel sodales justo. Phasellus scelerisque id ante ac fermentum. Curabitur ut vulputate nisl. Vestibulum et lorem erat. Phasellus at tempus lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Fusce rutrum blandit tortor, at congue odio suscipit eget. Nunc felis orci, fermentum id neque sit amet, tempor volutpat eros. Before You Use Context Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it makes component reuse more difficult. If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context. Aliquam eu dapibus sapien. Maecenas eu felis id nibh pellentesque ultrices sed at risus. Vivamus blandit, massa quis tempus convallis, arcu est vehicula sapien, vitae tristique purus metus sed dui. Curabitur ultricies libero varius accumsan mollis. Nullam venenatis magna lobortis, ornare nisl quis, mollis ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent quis quam et tortor commodo tincidunt. Creates a Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the tree. The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This default value can be helpful for testing components in isolation without wrapping them. Note: passing undefined as a Provider value does not cause consuming components to use defaultValue. Proin non dictum odio. Maecenas mattis augue eu mi pulvinar blandit. Integer iaculis non lacus vitae efficitur. In commodo, nisi ut rutrum rhoncus, massa sapien tempus nulla, et tincidunt nulla dolor a justo. Aenean euismod metus a varius bibendum. Suspendisse urna sem, auctor in cursus ac, pharetra ut elit. Praesent eget dignissim purus. Nulla eu neque pharetra, facilisis eros a, luctus turpis. Nullam venenatis, arcu nec condimentum ullamcorper, diam ante efficitur dolor, vitae accumsan nibh purus eget massa. Sed dignissim auctor viverra. Suspendisse placerat vehicula tortor, ut laoreet urna condimentum interdum. Proin libero diam, eleifend sed felis a, bibendum convallis nibh. Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes. The Provider component accepts a value prop to be passed to consuming components that are descendants of this Provider. One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree. All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes. The propagation from Provider to its descendant consumers (including .contextType and useContext) is not subject to the shouldComponentUpdate method, so the consumer is updated even when an ancestor component skips an update. Changes are determined by comparing the new and old values using the same algorithm as Object.is. A React component that subscribes to context changes. Using this component lets you subscribe to a context within a function component. Requires a function as a child. The function receives the current context value and returns a React node. The value argument passed to the function will be equal to the value prop of the closest Provider for this context above in the tree. If there is no Provider for this context above, the value argument will be equal to the defaultValue that was passed to createContext(). Because context uses reference identity to determine when to re-render, there are some gotchas that could trigger unintentional renders in consumers when a provider’s parent re- renders. For example, the code below will re-render all consumers every time the Provider re-renders because a new object is always created for value: INTRODUCTION & APIs What’s Context?
  • 6. “Context provides a way to pass data through the component tree without having to pass props down manually at every level.” - https://reactjs.org/docs/context.html “ Another way allowing you to implicitly share data (state) between components ” - me INTRODUCTION & APIs What’s Context?
  • 7. INTRODUCTION & APIs State sharing by passing props const Parent = () => { const state = 1; return ( <> <Child state={state} /> <Child state={state} /> <Child state={state} /> </> ); };
  • 8. Props Drilling 𑁋 Passing prop(s) down multiple levels INTRODUCTION & APIs Props Drilling const GrandParent = () => <Parent state={state} />; const Parent = () => <Child state={state} />; const Child = () => <GrandChild state={state} />; const GrandChild = () => <p>{state}</p>;
  • 9. INTRODUCTION & APIs Props Drilling - The problem Unnecessary re-renders in component tree Flashing border == Component re-renders
  • 10. INTRODUCTION & APIs Creating Context instance import { createContext } from "react"; const ColorContext = createContext(undefined);
  • 11. INTRODUCTION & APIs Context Provider const MainApp = () => { const [currentColor, setCurrentColor] = useState(""); return ( <ColorContext.Provider value={{ currentColor, setCurrentColor }}> <SetColorButtons /> <GrandParent /> </ColorContext.Provider> ); };
  • 12. INTRODUCTION & APIs Consuming Context import { useContext } from "react"; const SetColorButtons = () => { const colorContextValue = useContext(ColorContext); return ( ...buttons with onClick={() => colorContextValue.setCurrentShape(color)} ); };
  • 13. INTRODUCTION & APIs Consuming Context import { useContext } from "react"; const Color = () => { const colorContextValue = useContext(ColorContext); return ( <div style={{ background: colorContextValue.currentColor }}> Color: {color} </div> ); };
  • 14. INTRODUCTION & APIs Passing data with Context [Consumer] useContext(ColorContext) [Provider] <ColorContext.Provider>
  • 15. INTRODUCTION & APIs DIVE DEEPER & TIPS EXAMPLES
  • 16. DIVE DEEPER & TIPS Consuming Context Outside Provider const MainApp = () => { const [currentColor, setCurrentColor] = useState(""); return ( <> <GrandParent /> <ColorContext.Provider value={{ currentColor, setCurrentColor }}> <SetColorButtons /> <GrandParent /> </ColorContext.Provider> </> ); };
  • 17. DIVE DEEPER & TIPS Default Error Message
  • 18. DIVE DEEPER & TIPS Consuming Context Outside Provider const Color = () => { const colorContextValue = useContext(ColorContext); return ( <div style={{ background: colorContextValue.currentColor }}> Color: {color} </div> ); }; const ColorContext = createContext(undefined);
  • 19. DIVE DEEPER & TIPS Consuming Context Outside Provider const MainApp = () => { const [currentColor, setCurrentColor] = useState(""); return ( <> <GrandParent /> <ColorContext.Provider value={{ currentColor, setCurrentColor }}> <SetColorButtons /> <GrandParent /> </ColorContext.Provider> </> ); };
  • 20. DIVE DEEPER & TIPS Custom Error Message const Color = ({ color }) => { const colorContextValue = useContext(ColorContext); if (!colorContextValue) { throw new Error("ColorContext must be used within ColorProvider"); } return ( <div style={{ background: colorContextValue.currentColor }}> Color: {color} </div> ); };
  • 21. DIVE DEEPER & TIPS Abstraction const ColorProvider = ({ children }) => { const [currentColor, setCurrentColor] = useState(""); return ( <ColorContext.Provider value={{ currentColor, setCurrentColor }} > {children} </ColorContext.Provider> ); }; const MainApp = () => { return ( <ColorProvider> <SetColorButtons /> <GrandParent /> </ColorProvider> ); };
  • 22. DIVE DEEPER & TIPS Abstraction const useColor = () => { const colorContextValue = useContext(ColorContext); if (!colorContextValue) { throw new Error("useColor must be used within ColorProvider"); } return useContext(ColorContext); }; const Color = () => { const colorContextValue = useColor(); return ( <div style={{ background: colorContextValue.currentColor }}> Color: {color} </div> ); };
  • 23. DIVE DEEPER & TIPS Simple drawing app!
  • 24. DIVE DEEPER & TIPS Global state const PaintContext = createContext(undefined); const PaintProvider = ({ children }) => { const [currentShape, setCurrentShape] = useState(""); const [currentColor, setCurrentColor] = useState(""); const value = { currentShape, currentColor, setCurrentShape, setCurrentColor, }; return <PaintContext.Provider value={value}>{children}</PaintContext.Provider>; }; const usePaint = () => { const paintContextValue = useContext(PaintContext); if (!paintContextValue) { throw new Error("usePaint must be used within PaintProvider"); } return paintContextValue; };
  • 25. DIVE DEEPER & TIPS Provider at root level const App = () => { return ( <PaintProvider> <Header /> <MainLayout> <LeftPanel /> <RightPanel /> </MainLayout> </PaintProvider> ); }; const PaintPreview = () => { const { currentShape, currentColor } = usePaint(); return <RenderedObject shape={currentShape} color={currentColor} />; }; const ShapePreview = () => { const { currentShape } = usePaint(); return <RenderedObject shape={currentShape} />; }; const ColorPreview = () => { const { currentColor } = usePaint(); return <div>{currentColor}</div>; }; const ShapeSelection = () => { const { setCurrentShape } = usePaint(); return ...buttons with onClick={() => setCurrentShape(shape)} }; const ColorSelection = () => { const { setCurrentColor } = usePaint(); return ...buttons with onClick={() => setCurrentShape(color)} };
  • 26. DIVE DEEPER & TIPS Global state at root level [Provider] <PaintContext.Provider> [Consumer] const { currentShape,currentColor} = usePaint(); [Consumer] const { currentShape } = usePaint(); [Consumer] const { currentColor } = usePaint(); [Consumer] const { setCurrentShape } = usePaint(); [Consumer] const { setCurrentColor } = usePaint();
  • 27. DIVE DEEPER & TIPS Move Provider closer to where it’s needed const App = () => { return ( <> <Header /> <PaintProvider> <MainLayout> <LeftPanel /> <RightPanel /> </MainLayout> </PaintProvider> </> ); };
  • 28. ShapePreview & ShapeSelection does NOT need to re-render when color changed DIVE DEEPER & TIPS Problem with Global State ... Vice Versa
  • 29. DIVE DEEPER & TIPS Breaking global state into smaller related states const PaintContext = createContext(undefined); const PaintProvider = ({ children }) => { const [currentShape, setCurrentShape] = useState(""); const [currentColor, setCurrentColor] = useState(""); const value = { currentShape, currentColor, setCurrentShape, setCurrentColor, }; return <PaintContext.Provider value={value}>{children}</PaintContext.Provider>; }; const usePaint = () => { const paintContextValue = useContext(PaintContext); if (!paintContextValue) { throw new Error("usePaint must be used within PaintProvider"); } return paintContextValue; };
  • 30. DIVE DEEPER & TIPS Break into 2 Contexts const ShapeContext = createContext(undefined); const ShapeProvider = ({ children }) => { const [currentShape, setCurrentShape] = useState(""); const value = { currentShape, setCurrentShape }; return <ShapeContext.Provider value={value}>{children}</ShapeContext.Provider>; }; const useShape = () => { const shapeContextValue = useContext(ShapeContext); if (!shapeContextValue)... throw error ... return shapeContextValue; }; const ColorContext = createContext(undefined); const ColorProvider = ({ children }) => { const [currentColor, setCurrentColor] = useState(""); const value = { currentColor, setCurrentColor }; return <ColorContext.Provider value={value}>{children}</ColorContext.Provider>; }; const useColor = () => { const colorContextValue = useContext(ColorContext); if (!colorContextValue)... throw error ... return colorContextValue; };
  • 31. DIVE DEEPER & TIPS Abstraction layer (Optional) const PaintProvider = ({ children }) => { return ( <ShapeProvider> <ColorProvider>{children}</ColorProvider> </ShapeProvider> ); };
  • 32. DIVE DEEPER & TIPS Consuming Context separately const PaintPreview = () => { const { currentShape } = useShape(); const { currentShape } = useColor(); return <RenderedObject shape={currentShape} color={currentColor} />; }; const ShapePreview = () => { const { currentShape } = useShape(); return <RenderedObject shape={currentShape} />; }; const ColorPreview = () => { const { currentColor } = useColor(); return <div>{currentColor}</div>; }; const ShapeSelection = () => { const { setCurrentShape } = useShape(); return ...buttons with onClick={() => setCurrentShape(shape)} }; const ColorSelection = () => { const { setCurrentColor } = useColor(); return ...buttons with onClick={() => setCurrentShape(color)} };
  • 33. DIVE DEEPER & TIPS Consuming Context separately [Provider] <PaintContext.Provider> [Consumer] const { currentShape } = useShape(); const { currentColor } = useColor(); [Consumer] const { currentShape } = useShape(); [Consumer] const { setCurrentShape } = useShape(); [Consumer] const { currentColor } = useColor(); [Consumer] const { setCurrentColor } = useColor();
  • 34. DIVE DEEPER & TIPS Unnecessary re-render ShapeSelection has no reason to re-render …Same for ColorSelection
  • 35. DIVE DEEPER & TIPS Prevent re-render with React.memo import { memo } from "react"; const ShapeSelection = memo(() => { const { setCurrentShape } = useShape(); return ...buttons with onClick={() => setCurrentShape(shape)} });
  • 36. DIVE DEEPER & TIPS React.memo does not stop component from re-rendering
  • 37. DIVE DEEPER & TIPS Prevent re-render with React.memo import { memo } from "react"; const ShapeSelection = memo(() => { const { setCurrentShape } = useShape(); return ...buttons with onClick={() => setCurrentShape(shape)} }); ❌
  • 38. DIVE DEEPER & TIPS Unnecessary re-render
  • 39. DIVE DEEPER & TIPS Consuming Context const PaintPreview = () => { const { currentShape } = useShape(); const { currentShape } = useColor(); return <RenderedObject shape={currentShape} color={currentColor} />; }; const ShapePreview = () => { const { currentShape } = useShape(); return <RenderedObject shape={currentShape} />; }; const ColorPreview = () => { const { currentColor } = useColor(); return <div>{currentColor}</div>; }; const ShapeSelection = () => { const { setCurrentShape } = useShape(); return ...buttons with onClick={() => setCurrentShape(shape)} }; const ColorSelection = () => { const { setCurrentColor } = useColor(); return ...buttons with onClick={() => setCurrentShape(color)} };
  • 40. DIVE DEEPER & TIPS Split Context const ShapeProvider = ({ children }) => { const [currentShape, setCurrentShape] = useState(""); const value = { currentShape, setCurrentShape }; return <ShapeContext.Provider value={value}>{children}</ShapeContext.Provider>; };
  • 41. DIVE DEEPER & TIPS Split Context const ShapeGetterContext = createContext(undefined); const ShapeSetterContext = createContext(undefined); const ShapeProvider = ({ children }) => { const [currentShape, setCurrentShape] = useState(""); const getterValue = useMemo(() => ({ currentShape }), [currentShape]); const setterValue = useMemo(() => ({ setCurrentShape }), []); return ( <ShapeGetterContext.Provider value={getterValue}> <ShapeSetterContext.Provider value={setterValue}>{children}</ShapeSetterContext.Provider> </ShapeGetterContext.Provider> ); }; const useShapeGetter = () => { const shapeGetterContextValue = useContext(ShapeGetterContext); if (!shapeGetterContextValue) ... throw error ... return shapeGetterContextValue; }; const useShapeSetter = () => { const shapeSetterContextValue = useContext(ShapeSetterContext); if (!shapeSetterContextValue) ... throw error ... return shapeSetterContextValue; };
  • 42. DIVE DEEPER & TIPS Split Context
  • 43. DIVE DEEPER & TIPS Context Display Name in DevTool ShapeGetterContext.displayName = "ShapeGetterContext"; ShapeSetterContext.displayName = "ShapeSetterContext"; ColorGetterContext.displayName = "ColorGetterContext"; ColorSetterContext.displayName = "ColorSetterContext";
  • 44. INTRODUCTION & APIs DIVE DEEPER & TIPS EXAMPLES
  • 45. EXAMPLES Use cases ● Global State ○ ThemeProvider ○ LocaleProvider ● Compound Component [Flexible & Implicitly share state] ○ CollapsiblePanel
  • 46. EXAMPLES Use cases const PanelGetterContext = createContext(undefined); const PanelSetterContext = createContext(undefined); PanelGetterContext.displayName = "PanelGetterContext"; PanelSetterContext.displayName = "PanelSetterContext"; const usePanelState = () => { const context = useContext(PanelGetterContext); if (context === undefined) throw new Error("..."); return context; }; const usePanelStateToggle = () => { const context = useContext(PanelSetterContext); if (!context) throw new Error("..."); return context; }; const PanelLabel = ({ children }) => { const toggle = usePanelStateToggle(); return <div onClick={toggle}>{children}</div>; }; const PanelDetails = ({ children }) => { const open = usePanelState(); return open ? <div>{children}</div> : null; }; const Panel = ({ children }) => { const [open, setOpen] = useState(false); const toggle = useCallback(() => setOpen((o) => !o), []); return ( <PanelGetterContext.Provider value={open}> <PanelSetterContext.Provider value={toggle}>{children}</PanelSetterContext.Provider> </PanelGetterContext.Provider> ); }; <Panel> <label>Collapsible Panel</label> <PanelLabel> <h1>Label</h1> </PanelLabel> <PanelDetails> <h2>Details...</h2> <p>Details...</p> </PanelDetails> </Panel>
  • 47. INTRODUCTION & APIs Why… When... What... DIVE DEEPER & TIPS Do’s… Dont’s… Optimization... EXAMPLES Global State… Collapsible Panel...
  • 48. Q&A
  • 49. Thank you for listening Credits https://reactjs.org/docs/context.html https://giphy.com/explore/confused-travolta https://knowyourmeme.com/memes/feels-good

Hinweis der Redaktion

  1. This is sharing state. Passing prop to components is fundamentally state sharing Why do we need this context thing What’s a problem with passing props down like this?
  2. It becomes cumbersome, annoying, explicit and pollute to pass down multiples levels. I’m okay with prop drilling. So what’s the problem? It costs you some performance
  3. In this example Have Buttons for selecting color at root level in <MainApp/> Color as state Wanna render color in <Color/> way down in the tree In order to do that, passing down thru GP/P/C/GC When color changes Intermediate components have to re-render Even the color has nothing to do with them Only Color component needs to react to the color change Let’s see how Context will help solve this problem
  4. Use Color as state like previous example Call a function With Param: defaulValue (required in TS) Fallb ack When context can’t find matching provider up in the tree Return Context instance In this instance, it has a Provider component as a property
  5. Provider component requires value prop Value == context value So we pass a value as an object Containing; color and setter Wrap everything inside it like so
  6. useContext hook Passing your context instance Subscribe to change of value The ones that your just pass as a prop And Give you the value as a return value This component consume the context to get the setter function
  7. This component consume the context to get the color value <Color /> doestnot take any prop ===> implicitly share state
  8. That’s it intermediate components are no longer re-render
  9. What’s gonna happen
  10. Talk about defaultValue
  11. Value is undefined
  12. dont
  13. Improvement to error message More meaningful
  14. Cleaner provider
  15. Cleaner consumer Next: more complex example to see some more tips when using context
  16. Explain what is it doing Structure next?
  17. Explain parts We need Shape Value & setter Color Value & setter next?
  18. Wrap everything inside Provider Consume in many parts like so next?
  19. Render nicely? Intermediate comps dont rerender? Why header re-render? next?
  20. Provider doesn’t necessarily have to be at root level Wrap it closest to where it needed next?
  21. This can be consider two different functionality Don’t put unrelated state al together
  22. From what we see herer, We ONLY use shape in one And setter in another
  23. The way we consume it Consume whole object with value & setter Does not need to