07 Berry Conclusion
07 Berry Conclusion
07 Berry Conclusion
These docs are old and won’t be updated. Go to react.dev for the new React
docs.
These new documentation pages teach modern React and include live examples:
Your First Component
Passing Props to a Component
Components let you split the UI into independent, reusable pieces, and think
about each piece in isolation. This page provides an introduction to the idea of
components. You can find a detailed component API reference here.
}
}
The above two components are equivalent from React’s point of view.
Function and Class components both have some additional features that we will
discuss in the next sections.
Rendering a Component
2. React calls the Welcome component with {name: 'Sara'} as the props.
3. Our Welcome component returns a <h1>Hello, Sara</h1> element as
the result.
4. React DOM efficiently updates the DOM to match <h1>Hello,
Sara</h1>.
Note: Always start component names with a capital letter.
React treats components starting with lowercase letters as DOM tags. For
example, <div /> represents an HTML div tag, but <Welcome /> represents a
component and requires Welcome to be in scope.
To learn more about the reasoning behind this convention, please read JSX In
Depth.
Composing Components
Components can refer to other components in their output. This lets us use the
same component abstraction for any level of detail. A button, a form, a dialog, a
screen: in React apps, all those are commonly expressed as components.
For example, we can create an App component that renders Welcome many times:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome
name="Edite" /> </div>
);
}
Try it on CodePen
Berry Conclusion 307
Typically, new React apps have a single App component at the very top. However,
if you integrate React into an existing app, you might start bottom-up with a
small component like Button and gradually work your way to the top of the view
hierarchy.
Extracting Components
This component can be tricky to change because of all the nesting, and it is also
hard to reuse individual parts of it. Let’s extract a few components from it.
First, we will extract Avatar:
function Avatar(props) {
return (
<img className="Avatar" src={props.user.avatarUrl}
alt={props.user.name} /> );
}
The Avatar doesn’t need to know that it is being rendered inside a Comment. This
is why we have given its prop a more generic name: user rather than author.
We recommend naming props from the component’s own point of view rather
than the context in which it is being used.
We can now simplify Comment a tiny bit:
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<Avatar user={props.author} /> <div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
Next, we will extract a UserInfo component that renders an Avatar next to the
user’s name:
function UserInfo(props) {
return (
Berry Conclusion 309