React - Scrimba Course Flashcards

Learn React

1
Q

Why React?

A
  • They use the virtual DOM
  • Reusasable Web Components
  • Maintained by Facebook
  • Hirable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to import react?

A

import React, {Component} from “react”

import ReactDOM from “react-dom”

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

create-react-app

A
1. npm install create-react-app -g
<hr>
<b>2.create-react-app 'your-app-name'</b>
<hr>
3.cd 'your-app-name'
4.npm start
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

JSX

A

Javascript version of HTML
Stands for –
Javascript XML

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

How to render something to the page at the end of the document?

A

ReactDOM.render( whatDoWeWantToRender, toWhere)
or…
ReactDOM.render( <h1>Hello World!</h1>, document.getElementById(‘root’));

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

Functional Components

A

The reason they call it that because you create them by writing a function.

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

Component Naming Convetion

A
Component Names are Typically Capatalized with Camel Case
MyComponent()
or
ThisApp()
not
thisApp()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you send more than one element in a component?

A

You must wrap it in a div

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

Example of a functional component?

A
function MyApp() {
 return (
       JSX HERE
  )
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to add inline CSS to Component?

A

style = {{ color: ‘blue’ }}
also, if there is a dash change to camel case
for example
background-color into backgroundColor

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

Anywhere you are using JSX you need to…

A

import React from “react”

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

How do you export something as the end of the component file?

A

export default MyComponentName

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

It’s usually a good idea to name your seperate JS File the same as your….

A
Component Function Name...
Example - 
MyInfo.js
and
function MyInfo () {
}
export default MyInfo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to import your own module or file?

A

import MyInfo from “./MyInfo”
don’t forget the ./
You also don’t need the .js extension because it is the defaulted file extension

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

What is a good place to store all your components?

A

In a file named Components
So, now your import will be -
“./Components/MyInfo”

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

How do you add a class in JSX?

A

className=”class”
You can do this with react elements, and not the acutally components…
Like, don’t do this
No No No

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

How can you add CS on same page?

A
Create a new object named styles...
const styles = {
 color: "blue"
}
then later use the object in your JSX
h1 style="" -Yo-  h1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why would you add inline styles, instead of class with seperate CSS file?

A

If you want the styles to be dynamic and change them with JS.
For instance, the text color being different because the time of day.

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

Where do you store prop values?

A

Inside the Instance
for example
RenderComponent propName=”property”

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

How to access your props within your JS file?

A

add props into your functions as a variable
example
function Instance(props) {
}
then
{props.value} is the value from the instance occurrence

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

Self closing tag in JSX?

A
Different, because you must close them! 
-hr- 
becomes - 
hr / -
and so on...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Ternary Operators?

A

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement. If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

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

Ternary Examples:

BOOLEAN ? DoThisIfTrue : DoThisIfFalse

A
if(userIsYoungerThan21) {
  serveGrapeJuice();
}
else {
  serveWine();
}
This can be shortened with the ?: like so:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

In Javascript conditional operator can evaluate to an expression, not just a statement:

var userType = userIsYoungerThan18 ? “Minor” : “Adult”;

serveDrink(userIsYoungerThan21 ? “Grape Juice” : “Wine”);

They can even be chained:

userIsYoungerThan4 ?
serveMilk() : userIsYoungerThan21
? serveGrapeJuice() : serveWine();

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

Name 8 High Order Functions:

A
filter
map
sort
reduce
every
some
find
findindex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Explain filter

A

High Order Function

26
Q

Explain map

A

High Order Function - Method
First, you need an array, we’ll call it arrayOne.
Then you need to define a second array, we’ll call it arrayTwo.
Now you must make arrayTwo = arrayOne, like so…
let arrayTwo = arrayOne;
Now add the .map method on arrayOne like so…
let arrayTwo = arrayOne.map();
Now, .map runs a callback function inside of it, and it runs for as many times as the index for arrayOne, so if arrayOne = [0, 2, 4] then .map will fun three times, using the first argument as the value of the index….
so…
let arrayTwo = arrayOne.map( x => x+2) would make arrayTwo equal ====
[2, 4, 6]
Pretty cool!

27
Q

Explain sort

A

High Order Function

28
Q

Explain reduce

A

High Order Function

29
Q

Explain every

A

High Order Function

30
Q

Explain some

A

High Order Function

31
Q

Explain find

A

High Order Function

32
Q

Explain findindex

A

High Order Function

33
Q

How to make numbers into dollar strings?

A

number.toLocaleString(“en-US”, { style: “currency”, currency: “USD” })
or
(5234.342343)toLocaleString(‘en-US’, {style: ‘currency’, currency: “USD}) = “$5234.34”

34
Q

How to pull data from array from a different file, perhaps a json file, and load onto screen for how many items they have!

A

import React from “react”
import TodoItem from “./TodoItem”
import todosData from “./todosData”

function App() {
    const todoItems = todosData.map(item => return item )
    return (
        <div>
            {todoItems}
        </div>
    )
}

export default App

35
Q

Function into class based component example?

A
function App() {
    return (
        - div>
            - h1>Code goes here -/h1>
        -/div>    )}
vs
class App extends React.Component {
render {
return ( JSX
)}}
36
Q

Where should you put the <b>display logic</b> of a class based component?

A
Between render() { }
and return ( )
for example - 
render () {
    xx display logic xx
    return (
      JSX
    )
  }
37
Q

How do you call a method inside a render?

A

After render() { You would call it using the THIS keyword, for example —-

class App extends React.Component {

    yourMethodHere() {
        xxxxxxxxxxxxxxxxxxxxxxx
    }
    render() {
        const foo = this.yourMethodHere()
        return (
            <div>
                <h1 style="">Code goes here</h1>
            </div>
        )
    }
}
export default App
WARNING - THERE ARE CAVEATS WITH THIS!!!
38
Q

How do you call props inside a class based component?

A

{this.props.whatever}

39
Q

Can props change?

A

No, they are immutable.

40
Q

What is state in React?

A

Its a way to change it’s own data, and then refresh the DOM so that you can see the change visually.

41
Q

How do you define state?

A
constructor() {
        super()
        this.state = {
            answer: "Yes"
        }
    }
42
Q

How to toggle in state?

A

Conditional rendering

43
Q

Event Handling in React?

A

All of our event handlers are going to be the JS version with camel case convention.
onClick
onMouseOver
ect.
Full List -
https://reactjs.org/docs/events.html#supported-events

44
Q

How to add a method before render?

A

Just type in the name, initialize and define

handleClick() {
   console.log("I'm working!")
}
then comes render() { return ( JSX )}
within JSX call it by using: this.handClick - don't initialize...
45
Q

Do you change state directly?

A

No, you don’t modify the clothes you are already wearing. You take the old ones off, and put on new ones….

46
Q

What is setState?

A

It’s a method. 2 ways - 1st object literal -
this.setState( { x: “x” }) —
2nd
in order to access the old you state, you much pass it in as an argument, with the function as it’s first value…

this.setState((prevState){
return { count: prevState.count+1; }
})

47
Q

When you are using setState, don’t forget to…

A

bind it - inside the constructor method —-

this.handleClick = this.handleClick.bind(this)

48
Q

Events, when they fire, actually receive a…

A

event property… so it’s not good enough so say props.handleChange(props.item.id)…
you much call it with event? ->
onChange={(event) => props.handleChange(props.item.id)}

49
Q

componentDidMount () {

}

A

Life Cycle Method, inside class -
Essentially saying “you were just born!”
This component just mounted to the screen!
Mostly Used for API Calls
As soon as the component mounts, I can get the data I need to correctly display…

50
Q
componentWillReceiveProps (???) {
    if (???.whatever !== this.props.whatever) {
    ...do something important here
  }
}
DEPRICATED!!!!!!!!!!!!!!!!!!!!!!!!!!
A

Recieves a parameter called nextProps

DEPRICATED!!!!!!!!!!!!!!!!!!!!

51
Q

shouldComponentUpdate () {

}

A
Takes Two Args (nextProps, nextState)
Return true to update
Return false to not update
Making a decision whether you want to change your clothes or not... 
Gives us (the developers) a chance to optimize our code...
We are able to implement some logic to let the component determine whether it's even important enough to think about updating...
52
Q

React will render if…

A

If it has any question at all whether a component needs to rerender, it will RENDER!

53
Q

componentWillUnmount() {

}

A

Main use to do some clean or tear down of anything you set up that could potentially lead to clutter with the DOM or your application…
Most common use, if in componentDidMount() has an event listener, you could use this to remove event listener…

54
Q

componentWillMount() {
}
DEPRICATED

A

DEPRICATED

55
Q
componentWillUpdate() {
}
DEPRICATED
replaced with ---
getDerivedStateFromProps() { xxx }
A

DEPRICATED

56
Q

What is the only static life cycle?

It’s the one you probably don’t need…

https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

A
static getDerivedStateFromProps(props, state) {
        // return the new, updated state based upon the props
    }
57
Q

getSnapshotBeforeUpdate () {

}

A

Allows you to create a backup of the current way things are…
UNCOMMON

58
Q

Common way to use the ternary -

A

return props.isLoading ? -h1>Loading…-/h1> : -h1>Loaded -/h1>

59
Q

Example of setTimeout inside my Component -

A

You want to use componentDidMount() {} like so…

componentDidMount() {
        setTimeout(() => {
            this.setState({
                isLoading: false
            })
        }, 1500)
    }
60
Q

How to use the /&/& operator for conditional rendering?

A

with the && operator, the JS logic takes the first one, analyzes it, and just returns the second thing.
T && F - returns F
F && F - returns False, because 1st was F
T && T - return T
example -
{
this.state.unreadMessages.length > 0
&&
-h2>You have {this.state.unreadMessages.length} unread messages!-/h2>
}

61
Q

How to edit inline style in React?

A

xxxxxxxxxxxx