SlideShare a Scribd company logo
Context API
Lucas Lira Gomes <@x8lucas8x>
Software Engineer
Passionate Hacker
ArchLinux Zealot
FOSS Enthusiast
I’m fond of doing
things that last
and all that jazz.
Lucas Lira Gomes
2
contact@x8lucas8x.com
linkedin.com/in/x8lucas8x
facebook.com/x8lucas8x
youtube.com/X80lucas08X
twitter.com/x8lucas8x
x8lucas8x.com
github.com/x8lucas8x
Context API
● Built in
○ React 16.3
● Solution for
○ “Prop-drilling”
That rings a bell
● Idea is not novel
○ Experimental API
■ 16.X
● Mobx
○ @inject
● React-intl
○ @injectIntl
● React-redux
○ @connect
● React-router
○ @withRouter
API Overview
● React.createContext(defaultValue);
○ <Producer value={...}/>
○ <Consumer>
{context => (...)}
</Consumer>
Single Context
import React, { Component } from 'react';
const ContextA = React.createContext();
class ParentComponent extends Component {
state = {data: "SomeValue"};
render() {
return (
<ContextA.Provider value={this.state}>
{this.props.children}
</ContextA.Provider>
...
const ChildComponent = () => (
<ContextA.Consumer>
{context=> <h1>{context.data}</h1>}
</ContextA.Consumer>
);
Multiple Contexts
import React, { Component } from 'react';
const ContextA = React.createContext();
const ContextB = React.createContext();
class ParentComponent extends Component {
state = {data: "SomeValue"};
render() {
return (
<ContextA.Provider value={this.state.dataA}>
<ContextB.Provider value={this.state.dataB}>
{this.props.children}
</ContextB.Provider>
</ContextA.Provider>
...
const ChildComponent = () => (
<ContextA.Consumer>
{ctxA => (
<ContextB.Consumer>
{ctxB => <h1>{ctxA} or {ctxB}</h1>}
</ContextB.Consumer>
)}
</ContextA.Consumer>
);
Updating the Context
...
const ContextB = React.createContext();
class ParentComponent extends Component {
state = {
isClicked: false,
onClick: () => this.setState({isClicked: true})
};
render() {
return (
<ContextA.Provider value={this.state}>
{this.props.children}
</ContextA.Provider>
...
const ChildComponent = () => (
<ContextA.Consumer>
{context => (
<button onClick={context.onClick}>
Click me!
</button>
)}
</ContextA.Consumer>
);
Applications
● “Global” for a tree
○ Localisation
○ Routing
○ Theme
○ User
○ ...
Good Practices
Avoid re-renders
● Use the state
○ Referential identity
import React, { Component } from 'react';
const ContextA = React.createContext();
class ParentComponent extends Component {
render() {
return (
<ContextA.Provider value={{clicked: true}}>
{this.props.children}
</ContextA.Provider>
...
Keep it DRY
● HOCs to the rescue
import React, { Component } from 'react';
const WhateverContext = React.createContext();
export function withWhatever(Component) {
return function ThemedComponent(props) {
return (
<WhateverContext.Consumer>
{context => (
<Component
{...props}
whatever={context.whatever}
/>
)}
</WhateverContext.Consumer>
);
};
}
References
1. https://reactjs.org/docs/context.html
13
Context API
Lucas Lira Gomes <@x8lucas8x>

More Related Content

What's hot

React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
ManojSatishKumar
 
All about Context API
All about Context APIAll about Context API
All about Context API
Souvik Basu
 
React hooks
React hooksReact hooks
React hooks
Assaf Gannon
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Samundra khatri
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
Fabio Biondi
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Maulik Shah
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
valuebound
 
React js
React jsReact js
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
Vinícius Ribeiro
 
React Hooks
React HooksReact Hooks
React Hooks
Erez Cohen
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
Knoldus Inc.
 
React hooks
React hooksReact hooks
React hooks
Ramy ElBasyouni
 
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
Soluto
 
React web development
React web developmentReact web development
React web development
Rully Ramanda
 
State Management in Angular/React
State Management in Angular/ReactState Management in Angular/React
State Management in Angular/React
DEV Cafe
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
Yao Nien Chung
 
Redux toolkit
Redux toolkitRedux toolkit
Redux toolkit
ofoefiergbor1
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
Andrii Lundiak
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
All Things Open
 

What's hot (20)

React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
All about Context API
All about Context APIAll about Context API
All about Context API
 
React hooks
React hooksReact hooks
React hooks
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React js
React jsReact js
React js
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React Hooks
React HooksReact Hooks
React Hooks
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
React hooks
React hooksReact hooks
React hooks
 
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
 
React web development
React web developmentReact web development
React web development
 
State Management in Angular/React
State Management in Angular/ReactState Management in Angular/React
State Management in Angular/React
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Redux toolkit
Redux toolkitRedux toolkit
Redux toolkit
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 

Similar to Context API in React

First steps with Gazebo simulation for ROS
First steps with Gazebo simulation for ROSFirst steps with Gazebo simulation for ROS
First steps with Gazebo simulation for ROS
Sergey Matyunin
 
From shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practicesFrom shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practices
Ankush Chadha, MBA, MS
 
Intro to Flutter
Intro to FlutterIntro to Flutter
Intro to Flutter
Eason Pai
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
Jeremy Grancher
 
Introduzione a React Native - Facebook Developer Circle Rome
Introduzione a React Native - Facebook Developer Circle RomeIntroduzione a React Native - Facebook Developer Circle Rome
Introduzione a React Native - Facebook Developer Circle Rome
Matteo Manchi
 
Search in Django
Search in DjangoSearch in Django
Search in Django
Lucas Lira Gomes
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
Alessandro Molina
 
PyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development UpdatedPyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development Updated
Alessandro Molina
 
Tranquilizer
TranquilizerTranquilizer
Tranquilizer
Albert DeFusco
 
Ratpack 101 - GR8Conf 2015
Ratpack 101 - GR8Conf 2015Ratpack 101 - GR8Conf 2015
Ratpack 101 - GR8Conf 2015
Alvaro Sanchez-Mariscal
 
How To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptxHow To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptx
BOSC Tech Labs
 
Hands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShiftHands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShift
Amazon Web Services
 
Sep Nasiri "Upwork PHP Architecture"
Sep Nasiri "Upwork PHP Architecture"Sep Nasiri "Upwork PHP Architecture"
Sep Nasiri "Upwork PHP Architecture"
Fwdays
 
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Rockwell Automation
 
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Rockwell Automation
 
Creando microservicios con java micro profile y tomee - CUNORI 2020
Creando microservicios con java micro profile y tomee - CUNORI 2020Creando microservicios con java micro profile y tomee - CUNORI 2020
Creando microservicios con java micro profile y tomee - CUNORI 2020
César Hernández
 
RHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdfRHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdf
Harsh Shah
 
Ready player 2 Multiplayer Red Teaming Against macOS
Ready player 2  Multiplayer Red Teaming Against macOSReady player 2  Multiplayer Red Teaming Against macOS
Ready player 2 Multiplayer Red Teaming Against macOS
Cody Thomas
 
Tech Talk - Overview of Dash framework for building dashboards
Tech Talk - Overview of Dash framework for building dashboardsTech Talk - Overview of Dash framework for building dashboards
Tech Talk - Overview of Dash framework for building dashboards
Appsilon Data Science
 

Similar to Context API in React (20)

Mobx
MobxMobx
Mobx
 
First steps with Gazebo simulation for ROS
First steps with Gazebo simulation for ROSFirst steps with Gazebo simulation for ROS
First steps with Gazebo simulation for ROS
 
From shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practicesFrom shipping rpms to helm charts - Lessons learned and best practices
From shipping rpms to helm charts - Lessons learned and best practices
 
Intro to Flutter
Intro to FlutterIntro to Flutter
Intro to Flutter
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
Introduzione a React Native - Facebook Developer Circle Rome
Introduzione a React Native - Facebook Developer Circle RomeIntroduzione a React Native - Facebook Developer Circle Rome
Introduzione a React Native - Facebook Developer Circle Rome
 
Search in Django
Search in DjangoSearch in Django
Search in Django
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
 
PyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development UpdatedPyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development Updated
 
Tranquilizer
TranquilizerTranquilizer
Tranquilizer
 
Ratpack 101 - GR8Conf 2015
Ratpack 101 - GR8Conf 2015Ratpack 101 - GR8Conf 2015
Ratpack 101 - GR8Conf 2015
 
How To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptxHow To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptx
 
Hands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShiftHands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShift
 
Sep Nasiri "Upwork PHP Architecture"
Sep Nasiri "Upwork PHP Architecture"Sep Nasiri "Upwork PHP Architecture"
Sep Nasiri "Upwork PHP Architecture"
 
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
 
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
Exploring the Functionality of the Rockwell Automation® Library of Process Ob...
 
Creando microservicios con java micro profile y tomee - CUNORI 2020
Creando microservicios con java micro profile y tomee - CUNORI 2020Creando microservicios con java micro profile y tomee - CUNORI 2020
Creando microservicios con java micro profile y tomee - CUNORI 2020
 
RHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdfRHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdf
 
Ready player 2 Multiplayer Red Teaming Against macOS
Ready player 2  Multiplayer Red Teaming Against macOSReady player 2  Multiplayer Red Teaming Against macOS
Ready player 2 Multiplayer Red Teaming Against macOS
 
Tech Talk - Overview of Dash framework for building dashboards
Tech Talk - Overview of Dash framework for building dashboardsTech Talk - Overview of Dash framework for building dashboards
Tech Talk - Overview of Dash framework for building dashboards
 

Recently uploaded

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 

Recently uploaded (20)

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 

Context API in React

  • 1. Context API Lucas Lira Gomes <@x8lucas8x>
  • 2. Software Engineer Passionate Hacker ArchLinux Zealot FOSS Enthusiast I’m fond of doing things that last and all that jazz. Lucas Lira Gomes 2 contact@x8lucas8x.com linkedin.com/in/x8lucas8x facebook.com/x8lucas8x youtube.com/X80lucas08X twitter.com/x8lucas8x x8lucas8x.com github.com/x8lucas8x
  • 3. Context API ● Built in ○ React 16.3 ● Solution for ○ “Prop-drilling”
  • 4. That rings a bell ● Idea is not novel ○ Experimental API ■ 16.X ● Mobx ○ @inject ● React-intl ○ @injectIntl ● React-redux ○ @connect ● React-router ○ @withRouter
  • 5. API Overview ● React.createContext(defaultValue); ○ <Producer value={...}/> ○ <Consumer> {context => (...)} </Consumer>
  • 6. Single Context import React, { Component } from 'react'; const ContextA = React.createContext(); class ParentComponent extends Component { state = {data: "SomeValue"}; render() { return ( <ContextA.Provider value={this.state}> {this.props.children} </ContextA.Provider> ... const ChildComponent = () => ( <ContextA.Consumer> {context=> <h1>{context.data}</h1>} </ContextA.Consumer> );
  • 7. Multiple Contexts import React, { Component } from 'react'; const ContextA = React.createContext(); const ContextB = React.createContext(); class ParentComponent extends Component { state = {data: "SomeValue"}; render() { return ( <ContextA.Provider value={this.state.dataA}> <ContextB.Provider value={this.state.dataB}> {this.props.children} </ContextB.Provider> </ContextA.Provider> ... const ChildComponent = () => ( <ContextA.Consumer> {ctxA => ( <ContextB.Consumer> {ctxB => <h1>{ctxA} or {ctxB}</h1>} </ContextB.Consumer> )} </ContextA.Consumer> );
  • 8. Updating the Context ... const ContextB = React.createContext(); class ParentComponent extends Component { state = { isClicked: false, onClick: () => this.setState({isClicked: true}) }; render() { return ( <ContextA.Provider value={this.state}> {this.props.children} </ContextA.Provider> ... const ChildComponent = () => ( <ContextA.Consumer> {context => ( <button onClick={context.onClick}> Click me! </button> )} </ContextA.Consumer> );
  • 9. Applications ● “Global” for a tree ○ Localisation ○ Routing ○ Theme ○ User ○ ...
  • 11. Avoid re-renders ● Use the state ○ Referential identity import React, { Component } from 'react'; const ContextA = React.createContext(); class ParentComponent extends Component { render() { return ( <ContextA.Provider value={{clicked: true}}> {this.props.children} </ContextA.Provider> ...
  • 12. Keep it DRY ● HOCs to the rescue import React, { Component } from 'react'; const WhateverContext = React.createContext(); export function withWhatever(Component) { return function ThemedComponent(props) { return ( <WhateverContext.Consumer> {context => ( <Component {...props} whatever={context.whatever} /> )} </WhateverContext.Consumer> ); }; }
  • 14. Context API Lucas Lira Gomes <@x8lucas8x>