Components & Props Flashcards

1
Q

What are components?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Functional Components

A

The simplest way to define a component is to write a JavaScript function.

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Rendering a component

A

When React sees an element representing a user-defined component, it passes JSX attributes and children to this component as a single object. We call this object “props”.

Let’s recap what happens:

  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 <h1>Hello, Sara</h1> element as the result.
  4. React DOM efficiently updates the DOM to match <h1>Hello, Sara</h1>.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should react components always start with?

A

Always start component names with a capital letter. React treats components starting with lowercase letters as DOM tags. For example, <div></div> represents an HTML div tag, but represents a component and requires Welcome to be in scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Composing Components

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

App component

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Extracting components

A

Don’t be afraid to split components into smaller components. It can be tricky to change a component if it has a lot of nesting of JSX and it can be hard to reuse individual parts of it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How should you name props?

A

It’s recommended to name props from the component’s own pov rather than the context in which it is being used. 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 extracted to a separate component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Props are read-only

A

Whether you declare a component as a function or a class, it must never modify its own props. Consider this sum function:

function sum(a, b) {
  return a + b;
}

Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the single strict rule for React?

A

All React components must act like pure functions with respect to their props.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Props.children

A

Some components don’t know their children ahead of time. This is especially common for components like Sidebar or Dialog that represent generic “boxes”.
We recommend that such components use the special children prop to pass children elements directly into their output:

function FancyBorder(props) {
  return (
    <div>
      {props.children}
    </div>
  );
}

This lets other components pass arbitrary children to them by nesting the JSX:

function WelcomeDialog() {
  return (
      <h1>
        Welcome
      </h1>
      <p>
        Thank you for visiting our spacecraft!
      </p>

);
}

Anything inside the JSX tag gets passed into the FancyBorder component as a children prop. Since FancyBorder renders {props.children} inside a <div>, the passed elements appear in the final output.
</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly