Technical Frontend Interviews Topics: React.js

Technical Frontend Interviews Topics: React.js

1. React and State Management

State Management Hooks:

  • useState():

    • useState is a hook that manages state in functional components.

    • It returns an array containing the state variable and a setter function.

    • Example:

        const [count, setCount] = useState(0);
      
    • To update the state, call the setter function (setCount), and to use the state, refer to the state variable (count).

  • useEffect():

    • useEffect handles side effects in functional components.

    • It accepts a function and a list of dependencies.

    • The effect function runs after the component renders and re-runs when dependencies change.

    • Example:

           useEffect(() => {
                getCandidateInformation()
                    .then((response: CandidateModel) => {
                        setCandidate(response);
                        setIsLoading(false);
                    })
                    .catch((err) => {
                        console.error('Error loading candidate information:', err);
                        setError('Error loading candidate information.');
                        setIsLoading(false);
                    });
            }, []);
      

Array Transformation:

  • map():

    • map is a method that transforms each element of an array.

    • It takes a callback function and returns a new array.

    • Example:

        const numbers = [2, 4];
        const doubled = numbers.map(num => num * 2); // [4, 8]
      

Lifting State Up:

  • Centralize the state in a parent component.

  • Pass the state down to child components as props.

  • Child components can call functions passed as props to update the state in the parent component.

  • Example:

      function Parent() {
        const [state, setState] = useState(initialState);
        return <Child state={state} setState={setState} />;
      }
    

Context:

  • React Context:

    • Used for sharing state across the component tree without passing props down manually.

    • Useful for global settings like themes or localization.

    • Example:

        const ThemeContext = React.createContext('light');
      

Hooks:

  • useRef():

    • Used to access DOM elements directly.

    • Example:

        const inputRef = useRef(null);
      
  • useMemo():

    • Memoizes a computed value to optimize performance.

    • Example:

        const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
      

Redux:

  • Redux Lifecycle:

    • Components trigger actions.

    • Actions have types and payloads.

    • Reducers handle actions and update the state.

    • The state is passed to components via props.

    • Example:

        const action = { type: 'INCREMENT' };
      

React.js:

  • React is a JavaScript library for building user interfaces.

  • It is component-based, allowing for building reusable UI components.

  • Data is communicated from parent to child components using props.

useReducer():

  • Similar to useState but for more complex state logic.

  • Example:

      const [state, dispatch] = useReducer(reducer, initialState);
    

Component Lifecycle:

  • In React class components, lifecycle methods include componentDidMount, componentDidUpdate, and componentWillUnmount.

JavaScript Concepts:

  • Event Loop:

    • JavaScript is single-threaded but can handle asynchronous operations using the event loop.

    • Node.js extends JavaScript capabilities on the server side.

    • Example:

        setTimeout(() => console.log('Hello'), 1000);
      

2. Basic Pull Requests

Pull Requests:

  • Ensure the code is clean and well-documented.

  • Write tests for new features or changes.

  • Respond to comments constructively and make necessary improvements.

Handling Disagreements:

  • Discuss and justify your approach politely.

  • Be open to suggestions and willing to make changes.

Defining State in Class-Based Components:

  • Define the state inside the constructor.

  • Example:

      class MyComponent extends React.Component {
        constructor(props) {
          super(props);
          this.state = { count: 0 };
        }
      }
    

Hooks and Optimization:

  • useState():

    • Updating state triggers a re-render.
  • useMemo():

    • Avoid unnecessary re-computation.

    • Use it for expensive calculations.

    • Use for static components.

    • Example:

        const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
      

Referential Equality:

  • Comparing arrays and objects checks references, not values.

useRef() and useContext():

  • useRef():

    • Access DOM elements or persist values across renders.
  • useContext():

    • Share state across components without prop drilling.

WebSocket Protocol:

  • Real-time data fetching.

  • Example:

      const socket = io('http://localhost:3000');
    

Context API and Redux:

  • Context API:

    • Avoid overuse; useful for global data like themes.
  • Redux:

    • State management tool.

    • Example:

        const store = createStore(reducer);
      

WebSocket Protocol:

  • Real-time communication between client and server.

  • Example:

      const socket = io('http://localhost:3000');
    

Conclusion

Familiarity with state management, React hooks, Redux, and JavaScript fundamentals will prepare you for technical interviews and help you when building your own projects :)