React Flashcards
Vad används useEffect() för?
Skapa side effects i en komponent.
useState()
Funktion för att använda state.
Where to put state?
As close as possible to the component/s using that state.
What prop do list items need in react?
key (with a unique ID for every item)
How to use react fragments?
Wrap siblings in <> </> in jsx
Vilka parametrar har useEffect?
Två parametrar:
- En funktion att exekvera,
- En array med variabler som om de ändras ska trigga funktionen i första parametern.
Vilka varianter finns för andra parametern i useEffect?
- Om andra argumentet saknas så körs funktionen alltid.
- Om arrayen är tom så körs funktionen enbart när komponenten mountas.
- Om arrayen har ett eller flera värden/state, så körs funktionen när något av dem ändras.
What does useState return?
en array med två värden:
State
Och setState
How to call useState?
const [state, setState] = useState(initialState);
Whats the html attribute “class” called in jsx?
className
What’s the label element’s attribute “for” called in jsx?
htmlFor
How to make a controlled input?
Pass a value
prop with whatever state value you want the input to be. Use a handleChange to set state.
How to create a react component?
Write a JS-function that returns markup
How to nest react components?
Call a function that returns markup within another. Like this:
<Parent> <Child/> <Parent/>
How to add markup in react components?
JSX
How to add styles to react components?
- using the ‘className’ attribute to add css-classes
- using the ‘style’ sttribute to add inline styles depending on js-variables or expressions
How to display data in react components
Curly braces in JSX ‘escapes’ back in to js to interpolate variables or expressions.
How to render things conditionally?
Ternary operator if simple condition. Can be used in jsx because it’s an expression. Complex conditions are put in js and referenced from jsx with an expression.
What do lists in react need?
‘Key’ attribute with a unique value. React uses it to understand what is updated between renders.
How to respond to events in react?
Declare event handler functions in components. Pass them to Components listening for events.
How to update the screen in response to events in react?
Have your event handler functions set state and components will rerender i.e. update the screen.
How to share data between components in three steps?
- lift state to common parent
- pass props ` <MyComponent variable={variable} eventHandler={eventHandler}/> `
- read props ` function MyComponent({ variable, eventHandler }) `