1 Flashcards
(41 cards)
What is ES6
ES6 is a newer version of Javascript that allows concise and declarative syntax.
What is JSX
Javascript rendition of HTML
How do you import React?
import React from “react”
How do you import ReactDOM?
import ReactDOM from “react-dom”
How do you render to the DOM in React?
ReactDOM.render(<h1>code here</h1>. document.getElementById(“root”))
where root is the div references in your index.html
How can you add multiple html tags into the DOM?
Wrap them in a <div></div>
What is the syntax for a basic function?
function MyApp() { return (<h1>Hello World</h1>) }
Basic full example.
import React from “react”
import ReactDOM from “react-dom”
function MyApp() { return ( <div> <h1>Kevin Jones</h1> <p>I like turtles!</p> <p>I also like...</p> <ul> <li>Pizza</li> <li>Sushi</li> <li>KungFu</li> </ul> </div> ) }
ReactDOM.render(
,
document.getElementById(“root”)
)
How to export a function?
export default MyFunction
How to import a js or jsx from index.js?
import MyFunction from “./MyFunction”
how to add style to a js or jsx html elements?
Use className i.e. <h1> - a DOM API property
update styles.css</h1>
Syntax for basic arrow function, hello world? (useful for anon functions)
function HelloWorld = () => <h1>Hello world!</h1>
How to use variables in jsx?
Use curly braces… i.e. <h1>Hello {firstName} {lastName}</h1>
allows javascript execution. another example…
<h1>Hello {`${firstName} ${lastName}`}!</h1>
Example of dynamic variable rendering with React
function App() { const date = new Date() const hours = date.getHours() let timeOfDay
if (hours < 12) { timeOfDay = "morning" } else if (hours >= 12 && hours < 17) { timeOfDay = "afternoon" } else { timeOfDay = "night" }
return (
<h1>Good {timeOfDay}!</h1>
)
}
How to set background color on style tag with jsx? And font size?
Use backgroundColor: “#FF8C00” camel case syntax.
or fontSize: 24
Example dynamic styles.
import React from “react”
import ReactDOM from “react-dom”
function App() { const date = new Date(2018, 6, 31, 15) const hours = date.getHours() let timeOfDay const styles = { fontSize: 30 }
if (hours < 12) { timeOfDay = "morning" styles.color = "#04756F" } else if (hours >= 12 && hours < 17) { timeOfDay = "afternoon" styles.color = "#8914A3" } else { timeOfDay = "night" styles.color = "#D90000" }
return (
<h1 style="">Good {timeOfDay}!</h1>
)
}
ReactDOM.render(, document.getElementById(“root”))
How to read props objects, example ContactCard.js
import React from “react”
function ContactCard(props) {
console.log(props)
return (
<div>
<img></img>
<h3>{props.contact.name}</h3>
<p>Phone: {props.contact.phone}</p>
<p>Email: {props.contact.email}</p>
</div>
)
}
export default ContactCard
How to pass object values via props? App.js
import React from “react”
import ContactCard from “./ContactCard”
function App() { return ( <div>
</div> ) }
export default App
Check for existence of a specific variable and render dynamically?
Dynamic style using ternary
<h3>Question: {props.question}</h3>
Or…
<h3>Question: {props.question}</h3>
Example map function
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const doubled = nums.map(function(num) { return num * 2 }) console.log(doubled)
How to create const arrow function of data from JSON object. (using map function)
import React from “react”
import Joke from “./Joke”
import jokesData from “./jokesData”
function App() { const jokeComponents = jokesData.map(joke => )
return ( <div> {jokeComponents} </div> ) }
export default App
References for Javascript global objects (functions)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findindex
convert to specific currency string
<p>{props.product.price.toLocaleString("en-US", { style: "currency", currency: "USD" })}</p>
example of passing nested object from one component to another…
import React from “react”
import Product from “./Product”
import productsData from “./vschoolProducts”
function App() { const productComponents = productsData.map(item => )
return ( <div> {productComponents} </div> ) }
export default App
==== Product.js ======
import React from “react”
function Product(props) { return ( <div> <h2>{props.product.name}</h2> <p>{props.product.price.toLocaleString("en-US", { style: "currency", currency: "USD" })} - {props.product.description}</p> </div> ) }
export default Product
Code goes here
Code goes here
Is state important to know? {this.state.answer}
{this.state.name}
{this.state.age} years old
You are currently logged {wordDisplay}
Click me
{this.state.count}
Change!{this.state.count}
+1 -1{props.item.text}