React Intermediate Cheatsheet Review (Hooks, Programming Patterns, React Styles, Forms)

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

41 Terms

1
New cards

Q: What are Hooks in React?

A: Functions that let you "hook into" React features like state and lifecycle in function components.

2
New cards

Q: What are some advantages of using Hooks?

- Reuse stateful logic between components

- Separate concerns in code

- Avoid "this" keyword confusion

- Eliminate need for class constructors or method binding

3
New cards

Q: What are the two rules for using Hooks in React?

1. Only call Hooks inside React function components

2. Only call Hooks at the top level (not inside loops, conditions, or nested functions)

4
New cards

Q: Why shouldn't you call hooks conditionally or inside loops?

A: It can lead to inconsistent order of hook calls, which breaks the rules React relies on to manage state.

5
New cards

Q: What is the purpose of the useEffect() hook? Give 2 examples.

A: To perform side effects in function components (e.g., fetching data, DOM manipulation).

6
New cards

Q: What is the syntax for useEffect()?

A: useEffect(callback, dependencyArray)

7
New cards

Q: When is the effect in useEffect(() => { ... }) run if no dependency array is given?

A: After every render.

8
New cards

Q: When is the effect run with an empty dependency array?

A: Only once, after the first render.

9
New cards

Q: When is the effect run with specific dependencies?

A: When any value in the dependency array changes.

10
New cards

Q: What is the purpose of returning a function from useEffect()? Give two examples of what this can look like.

A: It's used as a cleanup function to prevent memory leaks (e.g., removing event listeners, clearing intervals).

11
New cards

Q: Can you use useEffect() multiple times in a component? Why would you do this (give 2 reasons)

A: Yes. This helps to separate concerns and manage different dependencies.

12
New cards

Q: What does the useState() hook do? Where is this hook called?

A: It adds state to a function component. It should be called at the top level of a React function definition.

13
New cards

Describe the structure of the code when you use the useState() hook.

const [currentState, stateSetter] = useState(initialState);

We have an array with two elements:

The current state value

A setter function to update the state.

Then, initialState is an optional value that can be used to set the value of currentState for the first render.

14
New cards

Q: When should you pass a function to a state setter like setCount()?

A: When the new state depends on the previous state.

15
New cards

Q: When is it okay to pass a value directly to the state setter?

A: When the new state does not depend on the previous one.

16
New cards

Q: Can useState() be used multiple times in one component? Give two reasons why or why not.

A: Yes. It allows for better organization and separation of different pieces of state.

17
New cards

What are side effects in React? (Give 3 examples)

A side effect in React is anything that affects something outside the scope of the current function/component rendering itself, such as:

- Fetching data from an API

- Manipulating the DOM directly

- Setting a timer

18
New cards

When the effect returns a cleanup function, when does it call this cleanup function?

The Effect Hook will call the cleanup function before calling the effect again as well as when the component is being removed from the DOM.

19
New cards

Q: What is a stateful component in React?

A: A component that manages and holds its own state using hooks like useState().

20
New cards

Q: What is a stateless component in React?

A: A component that does not manage its own state and usually relies on props passed from a parent.

21
New cards

Q: Can stateful or stateless components use props?

A: Yes, both stateful and stateless components can accept and use props.

22
New cards

Q: What is a common pattern for sharing state in React?

A: A stateful parent component passes state down to stateless child components via props.

23
New cards

Q: Can a React component change its own props?

No. Props are read-only and should be updated by the parent component.

24
New cards

Q: Can a React component update its own state?

A: Yes. A component can freely update its own state using the state setter function (e.g., setState, useState).

25
New cards

Can you call setState() with no argument and give example of what happens if you do.

No, you cannot just call setState() (or the setter function from useState) without providing a new value or an updater function as an argument.

E.g. Simply calling setCount() will set the state to undefined.

26
New cards

Q: Can a parent component pass a state-changing function to a child?

A: Yes. The function can be passed as a prop, allowing the child to trigger state updates in the parent.

27
New cards

Q: What happens when a state update function is called in a child component?

A: The parent re-renders, and the new state is passed down again as props.

28
New cards

Q: How are event handlers used with state in React?

A: Event handlers (e.g., onChange, onClick) receive an event object, which is often used to update component state.

29
New cards

Q: How do you access the value from an input field in a change handler?

A: Use event.target.value.

Example:

const [text, setText] = useState('');
const handleChange = (event) => {
setText(event.target.value);
};

30
New cards

Q: What is a container component in React? (List 3 aspects of it.)

A component that contains business logic, handles state, and passes data/behavior to presentational components.

31
New cards

Q: What is a presentational component?

A: A component focused on UI rendering, receiving data and behavior via props.

32
New cards

Q: Why use container and presentational component patterns? (Give 2 reasons)

A: It separates concerns and improves code maintainability.

33
New cards

Q: How do you apply inline styles to an element in React? Give an example.

A: By using the style prop and passing a JavaScript object.

Example:

const color = {
color: 'blue',
background: 'sky'
};
<h1 style={color}>Hello</h1>

34
New cards

Q: How can you apply inline styles directly without defining an object first? Give an example.

A: By passing an object inline to the style prop.

E.g.

<h1 style={{ color: 'red' }}>I am red!</h1>

35
New cards

Q: How are CSS property names written in React style objects?

A: In camelCase instead of kebab-case.

Example:CSS: font-size → React: fontSize

36
New cards

Q: How are style values written in React?

A: Mostly as strings, but numeric values are interpreted as pixels (px) by default.

E.g. fontSize: 20 is interpreted as '20px'

37
New cards

Q: What is an uncontrolled form field in React? Give example.

A: A form field that maintains its own state internally without being managed by React.

E.g.

const uncontrolledInput = <input />;

38
New cards

Q: What is a controlled form field in React? Give example.

A: A form field whose value is controlled by React state and updated via an onChange handler.

Example:

const controlledInput = (
<input value={stateValue} onChange={handleInputChange} />
);

39
New cards

Q: Which type of form field is generally preferred in React?

A: Controlled form fields are preferred because they allow React to manage the form state explicitly.

40
New cards

Q: What defines a controlled component in React?

A: A component where form inputs have their values bound to state and are updated via event handlers.

41
New cards

Q: What two props are essential for a controlled input field in React?

A: The value prop (bound to state) and the onChange handler (to update state).

Example:

<input value={stateValue} onChange={handleInputChange} />