Introduction to React Flashcards

1
Q

What is React?

A

Uses a syntax extension of JavaScript called Jsx that allows you to write HTML directly into JavaScript. JSX is similar to HTML but with some differences. JavaScript can be written in JSX like this { ‘this is treated as JavaScript code’ }.

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

What must nested JSX do?

A

Return a single element. This one parent element would wrap all of the other levels of nested elements.

Several JSX elements written with no parent wrapper will not work.

Here’s an example:

Valid JSX:

<div>
  <p>Paragraph One</p>
  <p>Paragraph Two</p>
  <p>Paragraph Three</p>
</div>
Invalid JSX:

<p>Paragraph One</p>

<p>Paragraph Two</p>

<p>Paragraph Three</p>

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

What is ReactDOM?

A

React’s rendering API that allows us to render JSX directly to the HTML DOM.
ReactDOM offers a simple method to render React elements to the DOM which looks like this: ReactDOM.render(componentToRender, targetNode), where the first argument is the React element or component that you want to render, and the second argument is the DOM node that you want to render the component to.

As you would expect, ReactDOM.render() must be called after the JSX element declarations, just like how you must declare variables before using them.

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

What are the key differences between JSX and HTML?

A
You can't use the word class in JSX to define a class. JSX instead uses className. In fact, the naming convention for all HTML attributes and event references in JSX become camelCase. For example, a click event in JSX is onClick, instead of onclick. Likewise, onchange becomes onChange.
It also differs in that every html element can be self closed and must be closed. For example the line break tag must always be written as <br>, but never as <br> because it contains no content. Div can be written as <div></div> or <div></div>. In the first we can't include anything in it and we'll learn why this is useful later.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a component?

A

“Simply put, a component is a JavaScript class or function that optionally accepts inputs i.e. properties(props) and returns a React element that describes how a section of the UI (User Interface) should appear.”

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

What is one of the ways to create a component in React?

A

Use a JavaScript function. This will create a functionless component. For now think of it as one that can receive data and render it, but does not manage or track changes to that data.

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

How do you create a component with a function?

A

To create a component with a function, you simply write a JavaScript function that returns either JSX or null. One important thing to note is that React requires your function name to begin with a capital letter. Here’s an example of a stateless functional component that assigns an HTML class in JSX:

// After being transpiled, the <div> will have a CSS class of 'customClass'
const DemoComponent = function() {
  return (
    <div></div>
  );
};</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s another way to create a react component?

A

Using the ES6 class syntax. In the following example, Kitten extends React.Component:

class Kitten extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <h1>Hi</h1>
    );
  }
}
This creates an ES6 class Kitten which extends the React.Component class. So the Kitten class now has access to many useful React features, such as local state and lifecycle hooks. Don't worry if you aren't familiar with these terms yet, they will be covered in greater detail in later challenges. Also notice the Kitten class has a constructor defined within it that calls super(). It uses super() to call the constructor of the parent class, in this case React.Component.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the constructor?

A

Special method used during the initialization of objects that are created with the class keyword. It is best practice to call a component’s constructor with super, and pass props to both. This makes sure the component is initialized properly. For now, know that it is standard for this code to be included. Soon you will see other uses for the constructor as well as props. Simply put, the constructor aids in constructing things.

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

How do you make a child components inside of a parent?

A
You would create a parent component which would render each of the other components as children. The child is written in custom HTML so it's written like this .
class MyApp extends Component {
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

You have to set three child components. How would you do it with syntax?

A

return (

)

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

Why is component composition so important in React?

A

Component composition is one of React’s powerful features. When you work with React, it is important to start thinking about your user interface in terms of components like the App example in the last challenge. You break down your UI into its basic building blocks, and those pieces become the components. This helps to separate the code responsible for the UI from the code responsible for handling your application logic. It can greatly simplify the development and maintenance of complex projects.

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

How are React Components passed into React.DOM.render()? Example?

A
React components are passed into ReactDOM.render() a little differently than JSX elements. For JSX elements, you pass in the name of the element that you want to render. However, for React components, you need to use the same syntax as if you were rendering a nested component, for example ReactDOM.render(, targetNode). You use this syntax for both ES6 class components and functional components.
Example: ReactDOM.render(, document.getElementById('challenge-node'));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

My first React Component that returns h1 to the browser

A
// Change code below this line
class MyComponent extends React.Component {
    constructor(props) {
        super(props);
    }
render() {
return(
    <div>
    <h1>My First React Component!</h1>
    </div>
);
}
};
ReactDOM.render(, document.getElementById('challenge-node'));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

My personal react component

A
class BeautifulTzuyu extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return(
            <div>
            <h1>Tzuyu is beautiful.</h1>
            </div>
        )
    }
}
ReactDOM.render(, document.getElementById('challenge-node'));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are props?

A

You can pass these properties to child components.

17
Q

Created this react component. Felt good about it

// Change code below this line
class MyComponent extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return(
            <div>
                <h1>
            My First React Component!
                </h1>
            </div>
        )
    };
}

ReactDOM.render(, document.getElementById(‘challenge-node’));

A

I’m approving. Keep moving forward

18
Q

How can we pass props to a Stateless Functional Component?

A

We can create custom HTML attributes that can be read. Let’s say we had a child component called Welcome in the parent component called App. We can do this

user is now apart of Welcome and we can use it as so
const Welcome = (props) =>  <h1>Hello, {props.user}!</h1>

So we basically we set that value when it’s the child in the parent. The value is set after so we use the props.value in the original component and actually call the value later. such as this:

function TzuyuPussy(props) {
  return <h1> Tzuyu has a {props.pussyHair} pussy. </h1>;
}
const element = ;
ReactDOM.render(element, document.getElementById('root'));
19
Q

How do you pass an array into a props?

A

To pass an array to a JSX element, it must be treated as JavaScript and wrapped in curly braces.

The child component then has access to the array property colors. Array methods such as join() can be used when accessing the property. const ChildComponent = (props) => <p>{props.colors.join(‘, ‘)}</p> This will join all colors array items into a comma separated string and produce: <p>green, blue, red</p> Later, we will learn about other common methods to render arrays of data in React.

20
Q

Explain default props

A
You can assign default props to a component as a property on the component itself and React assigns the default prop if necessary. This allows you to specify what a prop value should be if no value is explicitly provided. For example, if you declare MyComponent.defaultProps = { location: 'San Francisco' }, you have defined a location prop that's set to the string San Francisco, unless you specify otherwise. React assigns default props if props are undefined, but if you pass null as the value for a prop, it will remain null.
Example:
const ShoppingCart = (props) => {
  return (
    <div>
      <h1>Shopping Cart Component</h1>
    </div>
  )
};
// Change code below this line
ShoppingCart.defaultProps = {items: 0}
21
Q

When we pass arrays/integers into components we need to put it in { }. Why?

A

Remember that the syntax to add a prop to a component looks similar to how you add HTML attributes. However, since the value for quantity is an integer, it won’t go in quotes but it should be wrapped in curly braces. For example, {100}. This syntax tells JSX to interpret the value within the braces directly as JavaScript.

22
Q

Explain working with propTypes.

A

You can specify what type you want for a prop such as number, function, etc. It’s considered a best practice to set propTypes when you know the type of a prop ahead of time. You can define a propTypes property for a component in the same way you defined defaultProps. Doing this will check that props of a given key are present with a given type. Here’s an example to require the type function for a prop called handleClick:

MyComponent.propTypes = { handleClick: PropTypes.func.isRequired }

In the example above, the PropTypes.func part checks that handleClick is a function. Adding isRequired tells React that handleClick is a required property for that component. You will see a warning if that prop isn’t provided. Also notice that func represents function. Among the seven JavaScript primitive types, function and boolean (written as bool) are the only two that use unusual spelling. In addition to the primitive types, there are other types available. For example, you can check that a prop is a React element.

23
Q

Explain using this.props with class components.

A

The ES6 class component uses a slightly different convention to access props.

Anytime you refer to a class component within itself, you use the this keyword. To access props within a class component, you preface the code that you use to access it with this. For example, if an ES6 class component has a prop called data, you write {this.props.data} in JSX.
Example:
class ReturnTempPassword extends React.Component {
constructor(props) {
super(props);

}
render() {
return (
<div>
{ /* Change code below this line / }
<p>Your temporary password is: <strong>{this.props.tempPassword}</strong></p>
{ /
Change code above this line */ }
</div>
);
}
};

class ResetPassword extends React.Component {
  constructor(props) {
    super(props);

}
render() {
return (
<div>
<h2>Reset Password</h2>
<h3>We’ve generated a new temporary password for you.</h3>
<h3>Please reset this password from your account settings ASAP.</h3>
{ /* Change code below this line */ }

          { /* Change code above this line */ }
        </div>
    );
  }
};
24
Q

What is the difference between stateless functional components and stateful components.

A

A stateless functional component is any function you write which accepts props and returns JSX. A stateless component, on the other hand, is a class that extends React.Component, but does not use internal state (covered in the next challenge). Finally, a stateful component is a class component that does maintain its own internal state. You may see stateful components referred to simply as components or React components.

A common pattern is to try to minimize statefulness and to create stateless functional components wherever possible. This helps contain your state management to a specific area of your application. In turn, this improves development and maintenance of your app by making it easier to follow how changes to state affect its behavior.

class CampSite extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
      </div>
    );
  }
};
// Change code below this line
class Camper extends React.Component {
  constructor(props) {
    super(props);
  }
    render() {
      return (
        <div>
          <p>{this.props.name}</p>
          <p>Your pussy is way too fine</p>
        </div>
  )

} } Camper.defaultProps = { name: 'CamperBot'} Camper.propTypes = { name: PropTypes.string.isRequired }
25
Q

What is a state in React?

A

State consists of any data your application needs to know about, that can change over time. You want your apps to respond to state changes and present an updated UI when necessary. React offers a nice solution for the state management of modern web applications.

You create state in a React component by declaring a state property on the component class in its constructor. This initializes the component with state when it is created. The state property must be set to a JavaScript object. Declaring it looks like this:

this.state = {
  // describe your state here
}
You have access to the state object throughout the life of your component. You can update it, render it in your UI, and pass it as props to child components. The state object can be as complex or as simple as you need it to be. Note that you must create a class component by extending React.Component in order to create state like this.
26
Q

Explain State in terms of DOM and Virtual DOM

A

State is one of the most powerful features of components in React. It allows you to track important data in your app and render a UI in response to changes in this data. If your data changes, your UI will change. React uses what is called a virtual DOM, to keep track of changes behind the scenes. When state data updates, it triggers a re-render of the components using that data - including child components that received the data as a prop. React updates the actual DOM, but only where necessary. This means you don’t have to worry about changing the DOM. You simply declare what the UI should look like.

Note that if you make a component stateful, no other components are aware of its state. Its state is completely encapsulated, or local to that component, unless you pass state data to a child component as props. This notion of encapsulated state is very important because it allows you to write certain logic, then have that logic contained and isolated in one place in your code.

27
Q

What’s another way to render a method?

A

There is another way to access state in a component. In the render() method, before the return statement, you can write JavaScript directly. For example, you could declare functions, access data from state or props, perform computations on this data, and so on. Then, you can assign any data to variables, which you have access to in the return statement.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'freeCodeCamp'
    }
  }
  render() {
    // Change code below this line
      const name = this.state.name;
    // Change code above this line
    return (
      <div>
        { /* Change code below this line */ }
        <h1>{name}</h1>
        { /* Change code above this line */ }
      </div>
    );
  }
};