React - Scrimba Course Flashcards
Learn React
Why React?
- They use the virtual DOM
- Reusasable Web Components
- Maintained by Facebook
- Hirable
How to import react?
import React, {Component} from “react”
import ReactDOM from “react-dom”
create-react-app
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
JSX
Javascript version of HTML
Stands for –
Javascript XML
How to render something to the page at the end of the document?
ReactDOM.render( whatDoWeWantToRender, toWhere)
or…
ReactDOM.render( <h1>Hello World!</h1>, document.getElementById(‘root’));
Functional Components
The reason they call it that because you create them by writing a function.
Component Naming Convetion
Component Names are Typically Capatalized with Camel Case MyComponent() or ThisApp() not thisApp()
How do you send more than one element in a component?
You must wrap it in a div
Example of a functional component?
function MyApp() { return ( JSX HERE ) }
How to add inline CSS to Component?
style = {{ color: ‘blue’ }}
also, if there is a dash change to camel case
for example
background-color into backgroundColor
Anywhere you are using JSX you need to…
import React from “react”
How do you export something as the end of the component file?
export default MyComponentName
It’s usually a good idea to name your seperate JS File the same as your….
Component Function Name... Example - MyInfo.js and function MyInfo () { } export default MyInfo
How to import your own module or file?
import MyInfo from “./MyInfo”
don’t forget the ./
You also don’t need the .js extension because it is the defaulted file extension
What is a good place to store all your components?
In a file named Components
So, now your import will be -
“./Components/MyInfo”
How do you add a class in JSX?
className=”class”
You can do this with react elements, and not the acutally components…
Like, don’t do this
No No No
How can you add CS on same page?
Create a new object named styles... const styles = { color: "blue" } then later use the object in your JSX h1 style="" -Yo- h1
Why would you add inline styles, instead of class with seperate CSS file?
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.
Where do you store prop values?
Inside the Instance
for example
RenderComponent propName=”property”
How to access your props within your JS file?
add props into your functions as a variable
example
function Instance(props) {
}
then
{props.value} is the value from the instance occurrence
Self closing tag in JSX?
Different, because you must close them! -hr- becomes - hr / - and so on...
Ternary Operators?
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.
Ternary Examples:
BOOLEAN ? DoThisIfTrue : DoThisIfFalse
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();
Name 8 High Order Functions:
filter map sort reduce every some find findindex