Component - Context API
The Context API in React Native allows components to share data easily without passing props through every level. It helps manage global data like themes or user info across the app.
The Context API in React Native allows components to share data easily without passing props through every level. It helps manage global data like themes or user info across the app.
App ├─ ThemeContext.Provider │ └─ View │ ├─ Text (Theme: Light or Dark) │ └─ Button (Toggle Theme)
Context API?ChildComponent?
const MyContext = React.createContext('Default');
function Parent() {
return (
);
}
function ChildComponent() {
const value = React.useContext(MyContext);
return {value};
}const MyContext = React.createContext();
function Parent() {
return (
);
}
function Child() {
const value = React.useContext();
return {value};
}React.createContext(), wrap components with Provider passing user as value, use useContext in children.
B) Pass user as props to every component manually.
C) Use useState in every component to store user.
D) Use React.createContext() but do not wrap components with Provider.Pass the test and this pattern moves unknown