--- id: components-and-props title: Components and Props permalink: docs/components-and-props.html redirect_from: - "docs/reusable-components.html" - "docs/transferring-props.html" - "tips/props-in-getInitialState-as-anti-pattern.html" - "tips/communicate-between-components.html" prev: rendering-elements.html next: state-and-lifecycle.html --- Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen. ## Functional and Class Components The simplest way to define a component is to write a JavaScript function: ```js function Welcome(props) { return

Hello, {props.name}

; } ``` This function is a valid React component because it accepts a single "props" object argument with data and returns a React element. We call such components "functional" because they are literally JavaScript functions. You can also use an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) to define a component: ```js class Welcome extends React.Component { render() { return

Hello, {this.props.name}

; } } ``` The above two components are equivalent from React's point of view. Classes have some additional features that we will discuss in the [next sections](/react/docs/state-and-lifecycle.html). Until then, we will use functional components for their conciseness. ## Rendering a Component Previously, we only encountered React elements that represent DOM tags: ```js const element =
; ``` However, elements can also represent user-defined components: ```js const element = ; ``` When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object "props". For example, this code renders "Hello, Sara" on the page: ```js{1,5} function Welcome(props) { return

Hello, {props.name}

; } const element = ; ReactDOM.render( element, document.getElementById('root') ); ``` [Try it on CodePen.](http://codepen.io/gaearon/pen/YGYmEG?editors=0010) Let's recap what happens in this example: 1. We call `ReactDOM.render()` with the `` element. 2. React calls the `Welcome` component with `{name: 'Sara'}` as the props. 3. Our `Welcome` component returns a `

Hello, Sara

` element as the result. 4. React DOM efficiently updates the DOM to match `

Hello, Sara

`. >**Caveat:** > >Always start component names with a capital letter. > >For example, `
` represents a DOM tag, but `` represents a component and requires `Welcome` to be in scope. ## 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: ```js{8-10} function Welcome(props) { return

Hello, {props.name}

; } function App() { return (
); } ReactDOM.render( , document.getElementById('root') ); ``` [Try it on CodePen.](http://codepen.io/gaearon/pen/KgQKPr?editors=0010) 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. >**Caveat:** > >Components must return a single root element. This is why we added a `
` to contain all the `` elements. ## Extracting Components Don't be afraid to split components into smaller components. For example, consider this `Comment` component: ```js function Comment(props) { return (
{props.author.name}
{props.author.name}
{props.text}
{formatDate(props.date)}
); } ``` [Try it on CodePen.](http://codepen.io/gaearon/pen/VKQwEo?editors=0010) It accepts `author` (an object), `text` (a string), and `date` (a date) as props, and describes a comment on a social media website. 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`: ```js{3-6} function Avatar(props) { return ( {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: ```js{5} function Comment(props) { return (
{props.author.name}
{props.text}
{formatDate(props.date)}
); } ``` Next, we will extract a `UserInfo` component that renders an `Avatar` next to user's name: ```js{3-8} function UserInfo(props) { return (
{props.user.name}
); } ``` This lets us simplify `Comment` even further: ```js{4} function Comment(props) { return (
{props.text}
{formatDate(props.date)}
); } ``` [Try it on CodePen.](http://codepen.io/gaearon/pen/rrJNJY?editors=0010) Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. A good rule of thumb is that if a part of your UI is used several times (`Button`, `Panel`, `Avatar`), or is complex enough on its own (`App`, `FeedStory`, `Comment`), it is a good candidate to be a reusable component. ## Props are Read-Only Whether you declare a component [as a function or a class](#functional-and-class-components), it must never modify its own props. Consider this `sum` function: ```js function sum(a, b) { return a + b; } ``` Such functions are called ["pure"](https://en.wikipedia.org/wiki/Pure_function) because they do not attempt to change their inputs, and always return the same result for the same inputs. In contrast, this function is impure because it changes its own input: ```js function withdraw(account, amount) { account.total -= amount; } ``` React is pretty flexible but it has a single strict rule: **All React components must act like pure functions with respect to their props.** Of course, application UIs are dynamic and change over time. In the [next section](/react/docs/state-and-lifecycle.html), we will introduce a new concept of "state". State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.