React Basics Flashcards

1
Q

How do you create a new React app?

A

in command line:
mkdir FolderName
cd FolderName
npm init

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

How do you add a new module to your React app?

A

in command line:

npm install modulename –save

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

How do you create a server for your React app using the express module?

A
//create file under root directory
server.js
//in file server.js
var express = require('express');

var app = express();

app.use(express.static(‘public’));

app.listen(3000, function () {
console.log(‘Express server is up on port 3000’);
});

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

What argument does require(‘’) take?

A
The name of the module in a string
//example
//this gives us access to the entire express api
var express = require('express');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What does this line of code in in server.js?
var app = express();
A

Creates a new app that has access to the entire express library

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

How do you tell your React app which folder to user on the server?

A
app.use(express.static('public'));
//express.static('') tells the server which folder it will use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does app.listen() do and what two arguments does it take?

A
  • It starts the server

- 1. port name 2. a function that will be called when the server runs

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

What 2 things does the Babel library do for your React app?

A
  1. It allows you to use ES6 features for web browsers even if the browser does not include these features, Bable will compile them for you
  2. It converts JSX to javascript
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the react-dom library used for?

A

It is used applications in a web enviornment

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

How do you run the server for your React app?

A

node server.js

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

What are React components?

A

Building blocks for your regular app’s UI

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

What is the common naming convention for React components?

A
//first word is capitalized the rest are camelcase
//e.g.
ReactComponent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What argument does the React.createClass() function take and what does it do?

A
  1. it takes an options object

2. it describes the behavior of the class

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

What is the only thing required of a react component method?

A

render: function() {
return();
}

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

What syntax does the return(); method take in this render?
render: function() {
return();
}

A

jsx

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

Why wouldn’t this render?

var Greeter = React.createClass({
  render: function () {
    return (
      <div>
        <h1>Hello React!</h1>
        <p>This is form a component!</p>
      </div>
      <div>
        <p>This is another form a component!</p>
      </div>
    );
  }
});
A

The render will only render one root html element. In this example there are two separate divs when there should only be one. The only way to render the second div is to place the second div inside the first.

17
Q

What is the syntax for rendering a react component inside a render?

A
18
Q

What are component properties?

A

It’s a way to pass data into component when you first start it

19
Q

What is the syntax to pass a javascript variable in jsx

A

{ }

20
Q

How do you set a default prop?

A
getDefaultProps: function () {
    return {
      name: 'React',
      message: 'This is the default message!'
    };
  },
21
Q

How would you add a property called name to a component called Greeter

A
22
Q

What do you wrap JSX expressions that are longer than 1 line?

A

If a JSX expression takes up more than one line, then you should wrap the multi-line JSX expression in parentheses. This looks strange at first, but you get used to it:

(
  <a href="https://www.google.net">
    <h1>
      Click me I am Goooooogle
    </h1>
  </a>
)
23
Q

Basic JSX rules

A
  1. Only 1 outer element in a variable
24
Q

What is ReactDOM?

A

ReactDOM is the name of a JavaScript library. This library contains several React-specific methods, all of which deal with the DOM in some way or another.

25
Q

What is ReactDOM.render

A

ReactDOM.render is the most common way to render JSX. It takes a JSX expression, creates a corresponding tree of DOM nodes, and adds that tree to the DOM. That is the way to make a JSX expression appear onscreen.

26
Q

What happens when you try to update the DOM in React?

A
  1. The entire virtual DOM gets updated.
  2. The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed.
  3. The changed objects, and the changed objects only, get updated on the real DOM.
  4. Changes on the real DOM cause the screen to change.
27
Q

What does the JSX attribute className render as in html?

A

In HTML, it’s common to use class as an attribute name:

<h1>Hey</h1>

In JSX, you can’t use the word class! You have to use className instead:

<h1>Hey</h1>
This is because JSX gets translated into JavaScript, and class is a reserved word in JavaScript.

When JSX is rendered, JSX className attributes are automatically rendered as class attributes.

28
Q

What’s a self-closing tag?

A

Most HTML elements use two tags: an opening tag (<div>), and a closing tag (</div>). However, some HTML elements such as <img></img> and use only one tag. The tag that belongs to a single-tag element isn’t an opening tag nor a closing tag; it’s a self-closing tag.

When you write a self-closing tag in HTML, it is optional to include a forward-slash immediately before the final angle-bracket:

Fine in HTML with a slash:

<br></br>

Also fine, without the slash:

<br></br>
But!

In JSX, you have to include the slash. If you write a self-closing tag in JSX and forget the slash, you will raise an error:

Fine in JSX:

<br></br>

NOT FINE AT ALL in JSX:

<br></br>

29
Q

How do you execute regular JavaScript code in JSX?

A

You can do this by wrapping your code in curly braces.

{1+2}

30
Q

How are JSX event listeners like similar to HTML listeners like onclick written in JSX?

A

In JSX, event listener names are written in camelCase, such as onClick or onMouseOver.