Skip to main content

Command Palette

Search for a command to run...

Interview Discussions - Part 1

Updated
3 min read
  1. useRef to avoid re-rendering.

     import React, {useRef} from 'react';
    
     export default function App() {
       const name = useRef('');
    
       const handleName = (e) => {
          name.current = e.target.value
          document.getElementById('test').innerText = name.current
        };
    
       return(
         <>
          <input type="text" placeholder="Name" ref={name} onChange={handleName} />   
          <p id='test'></p>
         </>
       )
     }
    
  2. We can actually use callback function and get prevValue. This will increment `age` by 2

     import React, { useState } from 'react';
    
     export function App() {
       const [age, setAge] = useState(0);
    
       function handleClick() {
         setAge(currentAge => currentAge + 1);
         setAge(currentAge => currentAge + 1);
       }
    
       return (
         <div>
           <p>Current Age: {age}</p>
           <button onClick={handleClick}>Increase Age</button>
         </div>
       );
     }
    
  3. let and const variables are hoisted too but they cannot be accessed before their declarations.

  4. I used the definition 'A higher-order function is a function that takes one or more functions as arguments and returns a function as its result.' It will be 'or'. Also as far as i remember vaia shared Higher Order function will hide the implementation details.
    Ref: https://www.freecodecamp.org/news/higher-order-functions-in-javascript-explained/

  5. Difference between Git Merge and Git Rebase:

    Both git merge and git rebase are used to integrate changes from one branch into another, but they do so in different ways and are suited for different scenarios. Here's a breakdown of when to use each:

    Git Merge:

    What it does: git merge takes all the changes from one branch and merges them into another branch in a new commit.

    When to use it:

    1. Public Branches: For branches that are shared and where others are collaborating, like master or main, it's safer to use merge because it doesn't rewrite commit history.

    2. Merge Commits: If you want to preserve the history of branch merges explicitly with merge commits.

    3. Simplicity: If you're new to Git or are working in a team with varied Git experience, merges can be more straightforward to understand.

Git Rebase:

What it does: git rebase takes all the changes from one branch and "replays" them on top of another branch. This effectively rewrites the commit history to produce a linear sequence of commits.

When to use it:

  1. Linear History: If you prefer a clean, linear commit history without merge commits.

  2. Before a Pull Request: If you've been working on a feature branch and want to integrate the latest changes from the main branch before submitting a pull request, you can rebase your feature branch onto the main branch. This makes the history clearer and the PR easier to review.

  3. Local Cleanup: If you want to clean up or squash your local commits before pushing to a shared repository.

  4. Avoiding Merge Commits: If you don't want the extra merge commits that come from using git merge.

  5. Advanced Workflows: Rebase offers more advanced options like interactive rebasing, which allows for more granular control over commits (e.g., squashing, editing, reordering).

Important Considerations:

  1. Rewriting History: Rebasing rewrites commit history, which can be problematic for shared branches. If others have based work on the commits you're rebasing, it can create confusion. As a rule of thumb, never rebase branches that have been pushed to a shared repository unless you're certain of what you're doing and have communicated with your team.

  2. Merge Conflicts: Both merging and rebasing can result in merge conflicts, but they handle them differently. With merge, you'll address all conflicts in a single, separate merge commit. With rebase, you'll address conflicts commit-by-commit as Git replays each change.

  1. `git commit --amend` to push code changes to a specific commit without creating a new commit

More from this blog

Tanvir's Blog

14 posts