React/JavaScript/TypeScript Flashcards
Hook Rule: Only Call Hooks at the Top Level. | Hooks cannot be conditional
Why?
Do not call Hooks inside loops, conditions, or nested functions
React relies on the ORDER in which Hooks are called to associate Hook state and effects with the component rendering them. If you call Hooks conditionally or inside loops, it could break this order and lead to unexpected behaviors.
Hook Rule: Only Call Hooks from React Functions. Why?
DO NOT call Hooks from regular JavaScript functions. Instead, call Hooks from React function components, or from custom Hooks.
The state and effects from Hooks are persisted across renders. If you were to call a Hook conditionally or from a regular JavaScript function, React wouldn’t know how to preserve this state or which state corresponds to which Hook between renders.
This will help to ensure that your hooks are used correctly and that React can properly maintain the state of your components.
This ensures that all stateful logic in a component is clearly visible from its source code.
Use custom hooks to encapsulate complex logic.
This can help to make your code more readable and maintainable.
Hooks were designed to work with React’s rendering and diffing algorithms. Calling them outside the context of a React function component would not make sense and could lead to unpredictable behaviors.
Hook Rule: Keep the Body of a Hook Plain.
Why?
Avoid putting anything other than Hook calls and basic calculations in the body of your function component or custom Hook. Instead, use useEffect
for side effects, and use functions passed to setters (like setState(prevState => newState)
) for complex state logic.
Keeping the body of your Hooks and components plain helps prevent unnecessary re-renders and makes the components more predictable.
Hook Rule: Use the ESLint Plugin
Why?
Use the eslint-plugin-react-hooks
to enforce these rules.
This ESLint plugin helps catch violations of the rules, ensuring that your components adhere to best practices and that you avoid potential pitfalls.
Can you put useState
in an if function?
NO, you should not use useState
(or any other Hook) inside conditions, loops, or nested functions within a React component.
Explain Consistency and Predictability in Hook Execution Order
React relies on the order in which Hooks are called between multiple renders of a component to maintain the state and other associated values correctly.
If you put a Hook like useState
inside a condition, it might not get executed in some renders, leading to inconsistencies in the order of Hook calls between renders. This can cause unexpected behaviors and bugs in your component.
Ensuring Hooks are always called in the same order makes your components more predictable and easier to understand. It ensures that stateful logic is visible and consistent every time the component renders.
Is useState
async/sync and why or why not?
When you call the state update function returned by useState
, it schedules a re-render of the component, but it doesn’t force an immediate re-render synchronously.
If you have multiple setState
calls, React will group them together and re-render the component once, rather than re-rendering for every state change. This batching happens especially in event handlers and lifecycle methods in class components. This is done to optimize performance.
While useState
itself isn’t asynchronous, it’s important to remember that state updates might be asynchronous in nature. Therefore, when you’re updating the state based on the previous state, you should use the functional form of setState
:
setCount(prevCount => prevCount + 1);
This ensures that you’re always working with the most recent state value.
What is JSX?
JSX is a shorthand for JavaScript XML. This is a type of file used by React which utilizes the expressiveness of JavaScript along with HTML like template syntax. This makes the HTML file really easy to understand. This file makes applications robust and boosts its performance.
JSX gets compiled to REACT.CREATEELEMENT() calls which return plain JavaScript objects. It gives us expressiveness of JavaScript along with HTML like template syntax.
Why is class, className in React?
The reason why React uses className instead of class is because class is a reserved keyword in JavaScript
Describe data flow in React?
Data flow in React is unidirectional, which means that data can only flow from one component to another in a single direction. This helps to ensure that state is managed in a predictable and controlled way.
The main way data flows in React is through props. Props are passed from parent components to child components, and they can be used to pass data down the component hierarchy.
State is another way data can flow in React. State is data that is managed by a component, and it can be used to store things like the current user input or the current state of a component.
The data flow in React can be summarized as follows:
1. User interacts with UI.
2. UI component dispatches an event.
3. Event is handled by event handler.
4. Event handler updates state.
5. State is updated in component.
6. Component is re-rendered.
.
.
.
.
How is state updated?
Actions are a way of updating state. Actions are functions that are called to update the state of a component. When an action is called, it will usually dispatch an event, which will then be handled by the component’s state machine.
How do you delay an API call until a component is mounted?
In React, you often make API calls in the componentDidMount()
lifecycle method for class components or the useEffect()
hook for function components
The useEffect
hook takes two arguments: a function containing the code to run and a dependency array. The empty dependency array ([]
) ensures that the code inside the useEffect
runs only once, similar to componentDidMount
.
Explain the useEffect hook
The useEffect
hook allows you to run code after a component has mounted or when its props or state change.
The useEffect
hook takes two arguments: the first argument is a function that will be run when the component mounts or when its props or state change, and the second argument is an array of dependencies.
The array of dependencies is used to determine when the function will be run. If the array of dependencies is empty, the function will be run once when the component mounts. If the array of dependencies contains a prop or state value, the function will be run whenever that prop or state value changes.