Quiz

This quiz will test your knowledge on using React's context in a strongly typed manner in both function and class components.

Creating strongly-typed context

1

We are implementing a context for authentication. What is wrong with the implementation below and how can this be corrected?

type AuthContextType = {
  isAuthenticated: boolean;
  userName?: string;
  signIn: (userName: string) => void;
  signOut: () => void;
};
const AuthContext = React.createContext(undefined);
A)

Nothing is wrong with this implemenation.

B)

The type of the value for the context will be inferred as any. We can explicitly define the type for the context to create as follows:

const AuthContext = React.createContext<AuthContextType>(undefined);
C)

The type of the value for the context will be inferred as any. We can explicitly define the type for the context to create as follows:

const AuthContext = React.createContext<AuthContextType | undefined>(undefined);
Question 1 of 40 attempted

Get hands-on with 1400+ tech skills courses.