32
loading...
This website collects cookies to deliver better user experience
Want the ultimate guide to learn React from front to back? Check out The React Bootcamp.
App
, such as Header
, also have to pass the theme data down using props.export default function App({ theme }) {
return (
<>
<Header theme={theme} />
<Main theme={theme} />
<Sidebar theme={theme} />
<Footer theme={theme} />
</>
);
}
function Header({ theme }) {
return (
<>
<User theme={theme} />
<Login theme={theme} />
<Menu theme={theme} />
</>
);
}
theme
prop through multiple components that don’t immediately need it.Header
component doesn’t need theme
other than to pass it down to its child component. In other words, it would be better for User
, Login
and Menu
to consume the theme
data directly.createContext
method.value
prop.App
, let’s pass down our own name using Context and read it in a nested component: User
.import React from 'react';
export const UserContext = React.createContext();
export default function App() {
return (
<UserContext.Provider value="Reed">
<User />
</UserContext.Provider>
)
}
function User() {
return (
<UserContext.Consumer>
{value => <h1>{value}</h1>}
{/* prints: Reed */}
</UserContext.Consumer>
)
}
App
component, we are creating context with React.createContext()
and putting the result in a variable, UserContext
. In almost every case, you will want to export it as we are doing here because your component will be in another file. Note that we can pass an initial value to our value
prop when we call React.createContext()
.App
component, we are using UserContext
. Specifically UserContext.Provider
. The created context is an object with two properties: Provider
and Consumer
, both of which are components. To pass our value down to every component in our App, we wrap our Provider component around it (in this case, User
).UserContext.Provider
, we put the value that we want to pass down our entire component tree. We set that equal to the value
prop to do so. In this case, it is our name (here, Reed).User
, or wherever we want to consume (or use) what was provided on our context, we use the consumer component: UserContext.Consumer
. To use our passed down value, we use what is called the render props pattern. It is just a function that the consumer component gives us as a prop. And in the return of that function, we can return and use value
.React.useContext()
to consume context at the top of our component.import React from 'react';
export const UserContext = React.createContext();
export default function App() {
return (
<UserContext.Provider value="Reed">
<User />
</UserContext.Provider>
)
}
function User() {
const value = React.useContext(UserContext);
return <h1>{value}</h1>;
}
Avatar
component that requires two props username
and avatarSrc
from the App
component.export default function App({ user }) {
const { username, avatarSrc } = user;
return (
<main>
<Navbar username={username} avatarSrc={avatarSrc} />
</main>
);
}
function Navbar({ username, avatarSrc }) {
return (
<nav>
<Avatar username={username} avatarSrc={avatarSrc} />
</nav>
);
}
function Avatar({ username, avatarSrc }) {
return <img src={avatarSrc} alt={username} />;
}
App
, needs to know about the Avatar
component, we can create it directly within App
.avatar
, instead of two.export default function App({ user }) {
const { username, avatarSrc } = user;
const avatar = <img src={avatarSrc} alt={username} />;
return (
<main>
<Navbar avatar={avatar} />
</main>
);
}
function Navbar({ avatar }) {
return <nav>{avatar}</nav>;
}