1 of 26

Context

React Week 10

2 of 26

What is Context?

3 of 26

What is context?

A simpler way to pass props to multiple children or down multiple levels.

4 of 26

Context Creation

You must import the createContext function from react (or accessed by React.createContext)

The context should be exported from the file in which is it created and imported to where ever is using it.

5 of 26

Context Providers

A context provider should wrap the components that you want to access the context.

Children even multiple levels deep can access the value provided.

6 of 26

Context Consumer

Any child that is descended from the provider can call the consumer and access the value passed.

7 of 26

Exercise

Task: Instead of passing username into each component, get username from context

Link to CodeSandBox - https://codesandbox.io/s/context-exercise-g24id

8 of 26

useContext

9 of 26

What is context?

A simpler way to pass props to multiple children or down multiple levels.

10 of 26

Context Provider and Creation

Same as previously!

11 of 26

Context Consumer

You must import { useContext } from 'react' and also the context from the context creation��Assign the returned value to your variable

12 of 26

Exercise

Task: replace <UsernameContext.Consumer> with useContext()��Link to CodeSandBox - https://codesandbox.io/s/context-exercise-forked-f50x6

13 of 26

Context for Theming

14 of 26

<ThemeProvider> from Styled Components

When using styled components, you can use react context in a hook provided by Styled Components.

This Theme object will then be accessible in any styled components component!

15 of 26

Context Provider and Creation

Similar to previously!

Instead import ThemeProvider from ‘styled-components’

It is common to wrap the entire app

16 of 26

Accessing Theme

It is automatically passed to every styled component!

Sehr Easy

17 of 26

Exercise

  1. In addition to the existing context, wrap the entire App in the theme provider.
  2. Give it a primary color property of hotpink
  3. Replace anything ‘blue’ with the primary color from the ThemeProvider

Link to CodeSandBox - https://codesandbox.io/s/context-exercise-with-usecontext-68tco

18 of 26

useContext for Theming

19 of 26

What if you want to know what the primary color is outside of styled components?

If you have a functional component, it is super easy!�

20 of 26

But I want to change the theme dynamically!!

21 of 26

Context Provider and Creation

OK, so don't wrap the whole App.�Wrap everything that should have your theme and keep the state of your theme there.��Make sure you give it a default theme to work with

22 of 26

Changing The Theme

Treat it the same as any other setState that you get passed

23 of 26

Exercise

  • Create a component for changing the theme, it should contain:�1.1 A title that addresses the user by name �1.2 A Selector that changes the Theme on change
  • Move the theme provider to within the App component
  • Create a state for theme, pass the theme to the provider and the setTheme to the theme changer

Link to CodeSandBox - https://codesandbox.io/s/context-exercise-with-theme-forked-jxg5s

24 of 26

Resources

DOCS

25 of 26

Completed Exercise for if you get stuck

Link to CodeSandBox - https://codesandbox.io/s/context-exercise-with-theme-changer-66vi7?

26 of 26

Thank You!