ReactJS - beginner Flashcards

1
Q

How do you declare a new React component class?

A

You sub class React.Component

class MyComponentClass extends React.Component {
  render() {
    return <h1>Hello world</h1>;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the convention for writing Javascript class names ?

A

Upper Camel Case

for example: MyComponentClass

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

How can you tell the difference between JSX that is HTML-like and JSX that is a component instance?

A

Capitalisation.

< MyComponentClass /> // component instance

<h1>Hello there</h1>

// HTML-like JSX

In a JSX element, a capitalised first letter says, “I will be a component instance and not an HTML tag.”

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

How do you call a components render method?

A

You pass the component to ReactDOM.render(). ReactDOM.render calls the render method of MyComponentClass for you.

ReactDOM.render(
< MyComponentClass />,
document.getElementById(‘app’)
);

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

What are “named exports”?

A

Named exports are variables that you export using the export key word like this :

// file.js
export const name='Tom';

And then import them by their name within curly brackets like this:

// otherfile.js
import { name } from 'file.js';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a default export?

A

This is when a variable is exported from a file using the “default” keyword like this:

// file.js
const name="Tom"; 
export default name;

As this will be the default export when you import it you do not need to specify its name and can give the variable it will be placed into any name you want. The curly braces are not required for a default import.

// otherfile.js
import someName from 'file.js';

someName will now contain “Tom”;

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

What is this ?

< MyComponentClass />

A

An instance of a React component.

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

How do you view a components props?

A

You use this.props within the components class definition.

class PropsDisplayer extends React.Component {
render() {
const stringProps = JSON.stringify(this.props);
return (
<div>
<h1>CHECK OUT MY PROPS OBJECT</h1>
<h2>{stringProps}</h2>
</div>
);
}
}

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

What does JSON.stringify() do?

A

It turns a JavaScript object or value into a JSON string.

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

What does JSON.parse() do?

A

It turns a JSON string into a corresponding JavaScript variable.

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

Can a web browser read a Javascript file that contains JSX?

A

No a web browser can not read JSX. The JavaScript file will need to be compiled first with a JSX compiler. This will translate any JSX into regular JavaScript.

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

How can you write JSX code without using JSX?

A

Use React.createElement()

const h1 = <h1>Hello World</h1>;

// can be rewritten without JSX like this

const h1 = React.createElement(
    "h1",
    null,
    "Hello World"
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can JSX elements have attributes ?

A

Yes they can have attributes just like HTML elements.

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

Can you access variables declared outside of a JSX expression in a JSX expression?

A

Yes you can.

// Declare a variable
const name = "Tom";
// Access your variable from inside a JSX expression
const greeting = <p>Hello {name}!</p>;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you nest JSX elements?

A

Yes JSX elements can be nested just like HTML elements.

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

Can you use an if statement in a JSX expression?

A

No you cannot.

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

How do you make a JSX expression appear on screen?

A

You have to render it. From the “react-dom” library you can use ReactDOM.render().

18
Q

How do you add an event listener to a JSX element?

A

You give the JSX element a special attribute.

< img onClick={myFunc} />

19
Q

How do you get Javascript code to run that is inside JSX?

A

You wrap it in curly brackets.
The code below will render a h1 tag with 5 inside it.

ReactDOM.render(<h1>{2+3}</h1>, document.getElementById(‘app’));

20
Q

How do you import the react library?

A

import React from ‘react’;

This creates an object named React which contains methods necessary to use the React library.

21
Q

In a JavaScript file where can a JSX element be placed?

A

Anywhere that a JavaScript expression can go, for example, in an object:

const myTeam = {
manager: <li>Mark Rearden</li>,
apprentice1: <li>Thomas Mann</li>
}

22
Q

In a JSX element how is the event listener attribute’s name constructed?

A

It is constructed with “on” and the name of an “event”. For example “onMouseOver”.

23
Q

Is this HTML or JSX?

<p> Hello World </p>

A

It depends where it is. If it is in a HTML file it is HTML. If it is in a JavaScript file then it is JSX.

24
Q

Is this JavaScript or HTML?

conts h1 = <h1>Hello World</h1>;

A

It is JavaScript with some JSX!

25
Q

Read the answer aloud!!!!

A
opening tag, closing tag
opening tag, closing tag
opening tag, closing tag
opening tag, closing tag
opening tag, closing tag
26
Q

What are the arguments to the ReactDOM.render() method?

A

Argument 1 is a JSX expression. Argument 2 is a HTML element that the corresponding tree of nodes created from the JSX expression will be appended to.

27
Q

What are the methods for writing conditional statements within JSX expressions?

A

You can use the ternary operator and logical AND &&

28
Q

What do multi line JSX expressions require?

A

To be wrapped in parenthesis.

(
    <a href="https://www.whatever.com">
        Click me 
    </a>
)
29
Q

What do self closing tags require when written in JSX?

A

A forward slash immediately before the final angle bracket.

30
Q

What happens if you render the exact same content twice in a row?

A

The second render will do nothing. ReactDOM.render() only updates DOM elements that have changed.

31
Q

What is a important rule when creating JSX expression with multiple elements?

A

The first opening tag and the final closing tag must belong to the same element. A JSX expression needs a single outer element.

32
Q

What is JSX?

A

JSX is a syntax extension for JavaScript built to be used with React.

33
Q

What is React?

A

React is a JavaScript library.

34
Q

What is the virtual DOM?

A

The virtual DOM is a representation of the DOM that React uses to work out which real DOM elements need updating. Checking for updates in the virtual DOM is a lot faster than manipulating the real DOM.

35
Q

What will this return?

false && ‘hello there’;

A

It will return the value of the first falsy operand which is false.

36
Q

What will this return?

true && ‘hello there’

A

As both operands are true it will return the last operand ‘hello there’

37
Q

Who was React developed by?

A

Facebook engineers

38
Q

Can JSX elements use the class property?

A

No JSX requires the use of className instead of class. This is because class is a reserved word in JavaScript.

39
Q

How do you update state when state is an array?

A

You call the state setter and return a new array. You can place the contents of the old array into the new array by using the spread operator. You can then add any new elements as you normally would to an array.

const [things, setThings] = React.useState(["thing1", "thing2"]);
  function addThing(){
    setThings((prevState) => { 
      return [...prevState, `thing ${prevState.length + 1}`] });
  };
40
Q

How can you update state when state is an object?

A

You need to call the setter and return a new object. You can use the spread operator to return the original objects properties and then write new properties after this. If new properties already exist they will override the previous ones.

const [fields, setFields] = useState({ name: “”, email: “”, message: “”});

function updateName(name) {
    setFields(() => {
      return { ...fields, name: name };
    });
    console.log(fields);
  }
41
Q

What is the golden rule with state.

A

Never access state directly. Use the setter and avoid direct access such as

prevState++
prevState.push()
prevState.name=’newname’

42
Q

What is the best practice when accessing previous state?

A

As an argument to the state setting function you provide another function. This function will be provided with the previous state as an argument.

const [fields, setFields] = useState({ name: “”, email: “”, message: “”});

  function updateName(name) {
    setFields((prevState) => {
      return { ...prevState, name: name };
    });
    console.log(fields);
  }