State Flashcards

1
Q

Como utilizar state?

A

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>
    )
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Challenge State and ternary conditional

https://scrimba.com/learn/learnreact/challenge-flipping-state-back-and-forth-co4674229b54cd8335c75e54d

A

a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Add Element in Array (spread array)

https://scrimba.com/learn/learnreact/complex-state-arrays-co8f0498bb502fff29cbc8ee5

A

a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Updating state objects

https://scrimba.com/learn/learnreact/complex-state-updating-state-objects-cJLgWJSN

A

spread object
…prevObject,
//overide the attribute

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Como adiciona estilo inline como fazemos no HTML?

A

É 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>
))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adicione estilo de forma condicional:

https://scrimba.com/learn/learnreact/boxes-challenge-part-2-cobb94ca9aed421681cf67b3c

A

https://scrimba.com/learn/learnreact/boxes-challenge-part-2-cobb94ca9aed421681cf67b3c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How send ID to handle event function in component?

A

import React from “react”

export default function Box(props) {
    const styles = {
        backgroundColor: props.on ? "#222222" : "transparent"
    }
    return (
        <div style="">props.toggle(props.id)}
        >
        </div>
    )
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Forms and set values state to objects

A
//...
 const [formData, setFormData] = React.useState(
        {firstName: "", lastName: "", email: ""}
    )
    function handleChange(event) {
        setFormData(prevFormData => {
            return {
                ...prevFormData,
                [event.target.name]: event.target.value
            }
        })
    }
return (

//…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly