Skip to content

jcjmcclean/react-basic-context-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React basic context API example

Very basic example showing usage of React's context API.

The context API allows you to share data between components without worrying about prop drilling. The context API react docs can be found here: https://reactjs.org/docs/context.html

Component props

See this codesandbox for an example of the context api in use. You can see context being created and set in src/auth/auth.context.tsx and you can see how it's used and output in src/index.tsx.

At it's most basic, the context API is super simples:

To create a new context:

const ChantContext = React.createContext('wololo');

To use/access an existing context (anywhere in your app - will require imports):

const chantContext = React.useContext(ChantContext);