1 Flashcards
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”))