State Management Flashcards
What is shared state in React?
State used by multiple components.
What are the downsides of using prop drilling?
Forces components in between to have unused props, works for few adjacent components, not ideal for distant components.
What is React Context used for?
Sharing state across components.
How is React Context created?
By using createContext
, which requires a default value and has a generic type parameter.
What is the role of the Provider in React Context?
It supplies the context to child components.
How do you create a context in React?
const SomeContext = createContext<ContextType>(defaultValue);
How does a Provider component work in React Context?
It wraps child components and provides the context value using the SomeContext.Provider
.
How is the useContext
hook used in React?
It consumes context values by passing context into useContext
and destructuring properties.
What is the syntax to access context values using useContext
?
const { someState } = useContext(SomeContext);
How do you use context in a component?
By using useContext to access and display context values, like { someState }
.