State Flashcards
Como utilizar state?
import React, {useState} from “react”
ou
React.useState()
//Exemplo... //https://scrimba.com/learn/learnreact/usestate-changing-state-with-a-callback-function-cof9245f4a914e8aaf080c783
import React from “react”
export default function App() {
const [count, setCount] = React.useState(0)
function add() { setCount(function(oldValue) { return oldValue + 1 }) }
function subtract() { // Don't do this // setCount(count - 1) // Do that instead or the above option prevCount => prevCount - 1 }
return ( <div> – <div> <h1>{count}</h1> </div> \+ </div> ) }
Challenge State and ternary conditional
https://scrimba.com/learn/learnreact/challenge-flipping-state-back-and-forth-co4674229b54cd8335c75e54d
a
Add Element in Array (spread array)
https://scrimba.com/learn/learnreact/complex-state-arrays-co8f0498bb502fff29cbc8ee5
a
Updating state objects
https://scrimba.com/learn/learnreact/complex-state-updating-state-objects-cJLgWJSN
spread object
…prevObject,
//overide the attribute
Como adiciona estilo inline como fazemos no HTML?
É necessário adicionar o parâmetro style, da mesma forma que no HTML, porém este vai receber um objeto javascript:
const styles = { backgroundColor: props.darkMode ? "#222222" : "#cccccc" }
const squareElements = squares.map(square => ( <div style=""></div> ))
Adicione estilo de forma condicional:
https://scrimba.com/learn/learnreact/boxes-challenge-part-2-cobb94ca9aed421681cf67b3c
https://scrimba.com/learn/learnreact/boxes-challenge-part-2-cobb94ca9aed421681cf67b3c
How send ID to handle event function in component?
import React from “react”
export default function Box(props) { const styles = { backgroundColor: props.on ? "#222222" : "transparent" }
return ( <div style="">props.toggle(props.id)} > </div> ) }
Forms and set values state to objects
//... const [formData, setFormData] = React.useState( {firstName: "", lastName: "", email: ""} )
function handleChange(event) { setFormData(prevFormData => { return { ...prevFormData, [event.target.name]: event.target.value } }) }
return (
//…