React Flashcards
Como adicionar um elemento no DOM do react?
ReactDOM.render(elemento, selecionar onde será inserido)
ReactDOM.render(<p>olá, sou o Paulo</p>, document.getElementById(“root”);
// Challenge: Create your own custom React component! // Call it "MainContent", and have it return a simple // h1 element that says "I'm learning React!"
// Afterward, render it below the Navbar (which // you can do inside the ReactDOM.render call below)
function Navbar() { return (
<a href="#">Navbar</a> <span></span>
<div> <ul> <li> <a href="#">Home <span>(current)</span></a> </li> <li> <a href="#">Link</a> </li> <li> <a href="#"> Dropdown </a> <div> <a href="#">Action</a> <a href="#">Another action</a> <div></div> <a href="#">Something else here</a> </div> </li> <li> <a href="#">Disabled</a> </li> </ul>
Search </div> ) }
ReactDOM.render(
<div>
</div>, document.getElementById("root") )
function Navbar() { return (
<a href="#">Navbar</a> <span></span>
<div> <ul> <li> <a href="#">Home <span>(current)</span></a> </li> <li> <a href="#">Link</a> </li> <li> <a href="#"> Dropdown </a> <div> <a href="#">Action</a> <a href="#">Another action</a> <div></div> <a href="#">Something else here</a> </div> </li> <li> <a href="#">Disabled</a> </li> </ul>
Search </div> ) }
// Challenge: Create your own custom React component! // Call it "MainContent", and have it return a simple // h1 element that says "I'm learning React!"
// Afterward, render it below the Navbar (which // you can do inside the ReactDOM.render call below)
function MainContent() { return ( <h1>I'm learning React!</h1> ) }
ReactDOM.render(
<div>
</div>, document.getElementById("root") )
Why React?
É possível quebrar elementos do HTML em componentes.
É declarativo => O que precisa ser feito? Apenas me diga o que fazer e não se preocupe como eu vou fazer isso.
Imperativo é mais algo como faço isso? Me descreva cada paço de como fazer isso e eu farei.
// ReactDOM.render(<h1>Hello, React!</h1>, document.getElementById(“root”))
/*
Challenge - recreate the above line of code in vanilla JS by creating and
appending an h1 to our div#root (without using innerHTML).
- Create a new h1 element
- Give it some textContent
- Give it a class name of “header”
- append it as a child of the div#root
*/
const h1 = document.createElement(“h1”)
h1. textContent = “This is an imperative way to program”
h1. className = “header”
document. getElementById(“root”).append(h1)
O que é JSX?
É chamada JSX e é uma extensão de sintaxe para JavaScript. Recomendamos usar JSX com o React para descrever como a UI deveria parecer.
Como reenderizar mais de um componente com o ReactDOM?
// JSX const page = ( <div> <h1>This is JSX</h1> <p>This is a paragraph</p> </div> )
console.log(page)
ReactDOM.render(
page,
document.getElementById(“root”)
)
/*
Challenge: Starting from scratch, build and render the
HTML for our section project. Check the Google slide for
what you’re trying to build.
We’ll be adding styling to it later.
Hints:
* The React logo is a file in the project tree, so you can
access it by using src="./react-logo.png" in your image
element
* You can also set the
width` attribute of the image element
just like in HTML. In the slide, I have it set to 40px
*/
import React from “react”
import ReactDOM from “react-dom”
const page = ( <div> <img> <h1>Fun facts about React</h1> <ul> <li>Was first released in 2013</li> <li>Item 2</li> <li>Item 3</li> <li>Itme 4</li> </ul> </div> );
ReactDOM.render(page, document.getElementById(“root”))
O que é necessário para uma função javascript ser um componente do React?
Ter seu nome em camel case e a sintaxe em sua chamada: function TemporaryName() { return ( ) }
ReactDOM.render(, document.getElementById(“root”))
/** Challenge:
Part 1: Create a page of your own using a custom Page component
It should return an ordered list with the reasons why you’re
excited to be learning React :)
Render your list to the page
*/
import React from ‘react’
import ReactDOM from ‘react-dom’
function ListComponent() { return ( <ol> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ol> ) }
ReactDOM.render(, document.getElementById(“root”));
What is a React component?
A function that returns React elements. (UI)
import React from “react”
import ReactDOM from “react-dom”
/** Mini Challenge: Move the `header` element from Page into its own component called "Header" */
function Page() { return ( <div>
<img> <h1>Reasons I'm excited to learn React</h1> <ol> <li>It's a popular library, so I'll be able to fit in with the cool kids!</li> <li>I'm more likely to get a job as a developer if I know React</li> </ol> <small>© 2021 Ziroll development. All rights reserved.</small> </div> ) }
ReactDOM.render(, document.getElementById(“root”))
import React from “react”
import ReactDOM from “react-dom”
/** Mini Challenge: Move the `header` element from Page into its own component called "Header" */
function Header() { return (
<img> ) }
function Page() { return ( <div>
<h1>Reasons I'm excited to learn React</h1> <ol> <li>It's a popular library, so I'll be able to fit in with the cool kids!</li> <li>I'm more likely to get a job as a developer if I know React</li> </ol> <small>© 2021 Ziroll development. All rights reserved.</small> </div> ) }
ReactDOM.render(, document.getElementById(“root”))
/** Challenge: Project setup
- Create a
components
folder - Create the following components in separate files inside
the components folder. In each one, just render anh1
with the name of the component (e.g. return <h1>Navbar goes here</h1>):- Navbar
- Main
- Create an App component outside the components folder (sibling to
the index.js file)- Have App render the Navbar and Main components
- Import and render the App component inside of index.js using ReactDOM
- At this point you should have your “Navbar goes here” etc. showing up
in the mini-browser.
- At this point you should have your “Navbar goes here” etc. showing up
- Go to Google fonts and get the “Inter” font with weights 400, 600, and 700.
Put the links to those fonts ABOVE the style.css link in index.html (Use
the `` elements instead of the @import or npm options for getting
the fonts. You may need to do some extra research to figure out how this
works if you haven’t done it before)
*/
//components/Main.js import React from 'react'
export default function Main() { return ( <h1>Main goes here</h1> ) }
//components/Navbar.js import React from 'react' export default function Navbar() { return ( <h1>Navbar goes here</h1> ) }
//App.js import React from 'react' import Main from './components/Main' import Navbar from './components/Navbar'
export default function App() {
return (
<div>
</div> ) }
//index.js import React from 'react' import ReactDOM from 'react-dom' import Main from './components/Main' import Navbar from './components/Navbar' import App from './App'
ReactDOM.render(, document.getElementById(“root”))
Como exibir um elemento apenas se ele for recebido pelo componente do React?
Exemplo para a propriedade setup
{props.setup && <h3>Setup: {props.setup}</h3>}
<h3>Setup: {props.setup}</h3>
Como passar parâmetros javascript como props para um componente do react?
Utilize map em arrays de dados
https://scrimba.com/learn/learnreact/mapping-components-co20249b69609b9df5473acc7
export default function App() { const jokeElements = jokesData.map(joke => { return }) return ( <div> {jokeElements} </div> ) }