Skip to main content

Command Palette

Search for a command to run...

React Context API Default value

Updated
1 min readView as Markdown
React Context API Default value

auth-context.js

import React from 'react';

// AuthContext is an object that contain a component
//  This default value will actually only be used if we
// would consume without having a provider or provider tag

const AuthContext = React.createContext({
    isLoggedIn: false             //default value
});

export default AuthContext;

The way React works and the way Visual Studio Code at least works is they looking at this default context object to find out what you are able to access on your context. So for better IDE auto-completion, it is a good idea to add isLoggedIn here in your default context object as well just with a dummy value because we're not going to use this anyways. And then you will see that now I get to this isLoggedIn auto-completion. So that's just a tiny hint you might consider because it can be helpful.

More from this blog

Omar's blog

135 posts