Context Variables

Use context variables to pass down data with Transcrypt.

Introducing context variables

When passing props down multiple levels to where they are needed, it can involve a lot of repetitive code, and it can be tedious to keep track of them. To help in this situation, React has a set of methods that can be used to create the equivalent of what would be component-scoped global props. That is, a context variable that works just like component props, but can be accessed anywhere it is created in the component tree below. We use this mechanism so a prop doesn’t need to be passed down explicitly through any intermediate components in order to be utilized at a lower level in the component tree. The first thing we need to do is to update our pyreact.py module to include the necessary React methods. So, let’s add React.createContext() and React.useContext() to that file.

Get hands-on with 1200+ tech skills courses.