React Flashcards

1
Q

Vad används useEffect() för?

A

Skapa side effects i en komponent.

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

useState()

A

Funktion för att använda state.

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

Where to put state?

A

As close as possible to the component/s using that state.

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

What prop do list items need in react?

A

key (with a unique ID for every item)

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

How to use react fragments?

A

Wrap siblings in <> </> in jsx

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

Vilka parametrar har useEffect?

A

Två parametrar:
- En funktion att exekvera,
- En array med variabler som om de ändras ska trigga funktionen i första parametern.

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

Vilka varianter finns för andra parametern i useEffect?

A
  1. Om andra argumentet saknas så körs funktionen alltid.
  2. Om arrayen är tom så körs funktionen enbart när komponenten mountas.
  3. Om arrayen har ett eller flera värden/state, så körs funktionen när något av dem ändras.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does useState return?

A

en array med två värden:
State
Och setState

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

How to call useState?

A

const [state, setState] = useState(initialState);

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

Whats the html attribute “class” called in jsx?

A

className

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

What’s the label element’s attribute “for” called in jsx?

A

htmlFor

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

How to make a controlled input?

A

Pass a value prop with whatever state value you want the input to be. Use a handleChange to set state.

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

How to create a react component?

A

Write a JS-function that returns markup

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

How to nest react components?

A

Call a function that returns markup within another. Like this:

<Parent>
  <Child/>
<Parent/>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add markup in react components?

A

JSX

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

How to add styles to react components?

A
  1. using the ‘className’ attribute to add css-classes
  2. using the ‘style’ sttribute to add inline styles depending on js-variables or expressions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to display data in react components

A

Curly braces in JSX ‘escapes’ back in to js to interpolate variables or expressions.

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

How to render things conditionally?

A

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.

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

What do lists in react need?

A

‘Key’ attribute with a unique value. React uses it to understand what is updated between renders.

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

How to respond to events in react?

A

Declare event handler functions in components. Pass them to Components listening for events.

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

How to update the screen in response to events in react?

A

Have your event handler functions set state and components will rerender i.e. update the screen.

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

How to share data between components in three steps?

A
  1. lift state to common parent
  2. pass props ` <MyComponent variable={variable} eventHandler={eventHandler}/> `
  3. read props ` function MyComponent({ variable, eventHandler }) `
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to pass a function with arguments as a callback?

A

Wrap it in an arrow function.

'onclick' = () => myFunction(arg)
24
Q

The five steps of implementing a UI in react?

A
  1. Draw boxes on a mockup and name your components
  2. Build static version
  3. Identify state
  4. Identify WHERE to put state
  5. Add inverse data flow.
25
Q

How to break the react UI into component hierarchy?

A
  • draw the UI, make boxes around components and name them
  • make tree of hierarchy (what components are nested in other components)
26
Q

How to build a static version in React?

A
  • Build components that reuse other components and pass data with props
  • Bigger project - build bottom-up
  • Smaller projects - build top-down
27
Q

How to find the minimal but complete representation of UI state?

A

Identify what isn’t state:
1. Remains unchanged
2. Passed in from parent component
3. Computed based on something else

28
Q

How to identify where state should live?

A
  1. Identify every component that renders something based on that state
  2. Find closest common parent to them
    • Put state directly in common parent
      - Put state in some component above the common parent
      - If nothing else makes sense create a new component to hold the state somewhere above the common parent component.
29
Q

How to add inverse data flow in react ui?

A
  1. setState based on user input
  2. Pass functions to setState down from stateful component to components recieving user input
30
Q

Vad är redux?

A

Ett JS-bibliotek för hantering av globalt state (i react-applikationer)

31
Q

Hur representerar redux state?

A

Ett omutbart (readonly) js-objekt.

32
Q

Vad resulterar en förändring i state i redux i?

A

Ett nytt state-objekt som innehåller de förändringar som gjorts.

33
Q

Hur uppdateras state i redux?

A

Via actions

34
Q

Vad är en ‘action’ i redux?

A

En action i Redux ett vanligt objekt med en obligatorisk type-egenskap och valfritt ytterligare data (payload), som tillsammans beskriver en händelse som leder till en förändring av applikationens tillstånd.

35
Q

Vad är reducer-funktionen i redux?

A

Den funktion som körs när en action dispatchas från applikationen. Beroende på type-egenskapen i action justeras state.

36
Q

State mutations in redux must be…

A

Pure functions taking in current state and the action being dispatched returning a new state, combining the two.

37
Q

What argument does Redux.createStore take?

A

A reducer function to run whenever an action gets dispatched.

38
Q

What is the set-back of prop drilling and overuse of context?

A

It can lead to code that is difficult to maintain. Especially in larger applications.

39
Q

How to overcome problems with prop drilling and overuse of context?

A

Composition. Structuring components in a more modular and flexible way.

40
Q

What is prop drilling?

A

Passing props way down through the component tree to the component that actually uses it. Makes code hard to follow.

41
Q

What is Context overuse?

A

An native react API for passing data deep into the component tree without prop drilling. Using it too much leads to tight coupling and makes code hard to refactor. Can also lead to performance issues if a lot of components are consuming context.

42
Q

What is composition in react?

A

It is a pattern where you build components that can be reused and combined in different ways. Making it possible to only pass the necessary props to each component.

43
Q

What is “Render props” or “Children as function”?

A

A react design pattern that allows you to pass a function as props or children to a component. The component calls the function to dynamically determine what should be rendered.

44
Q

Two advantages of render props (or children as functions)

A
  1. Flexible rendering: eg. only render the child if data is available
  2. Separation of concerns: the parent component is responsible only for retrieving or providing data and the child component (passed as a function asachildren or props to parent) is responsible for rendering UI based on data provided by parent.
45
Q

How does render props or children as function avoid prop drilling?

A

By having a parent component be responsible for directly fetching the data needed and a child - passed as a function to the parent - which renders something based on the data.

46
Q

Two use cases for render props or children as function?

A
  1. When there are components that share behaviour (like fetching data), but need to render differently depending on situation
  2. When you want to avoid prop drilling by keeping data handling logic encapsulated in a parent component and letting render logic be handled by child component.
47
Q

A classic way to write a dataprovider-component (recieving) children as function?

A
const dataProvider = ({children}) => {
    const data = fetchData() //  Some function to actually fetch the data
    return children(data) // Return a call to the `children` function passed
} 
48
Q

How to use a dataprovider-component?

A
<DataProvider>
    {(data) => <ChildComponentName data = {data} /> }
<DataProvider/>
49
Q

What’s the difference between render props and children as function?

A

They are closely related react-patterns with the same goal of avoiding prop drilling. The difference is that a render prop-component takes a prop that is a function while a children as function-component uses the children prop itself as a function.

Render prop:

```
<DataProvider render={(data) => <DisplayData data={data} />} />
~~~

Children as function:

```

<DataProvider>
{(data) => <DisplayData data={data} />}
</DataProvider>

~~~

50
Q

What’s a higher order component (HOC)?

A

A function that takes a component and returns a new component with additional props.

51
Q

What’s the advantage of HOCs?

A

They allow you to add functionality to a component without modifying them directly.

52
Q

How does a HOC work?

A

It’s a function that takes a component as argument and returns a new function component with some alteration.

53
Q

How to use a HOC?

A

Assign it’s result to a const that can be used:

const NewAlteredComponent = higherOrderComponent(ComponentToAlter);

Now you can use <NewAlteredComponent/> in the react app with any additional props.

54
Q

Two reasons to use HOCs?

A
  1. Separate concerns (data fetching (HOC) from presentation (component) ).
  2. Reuse the HOC across multiple components that need same data.
55
Q

What is Component Inversion in react?

A

A pattern involving using controlled components. For example having a searchfield-component that takes a function for handling changes and update state in its parent. This makes the component more reuseable than having it handle the search itself.