Interview Discussions - Part 1
useRefto 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> </> ) }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> ); }let and const variables are hoisted too but they cannot be accessed before their declarations.
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/Difference between Git Merge and Git Rebase:
Both
git mergeandgit rebaseare 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 mergetakes all the changes from one branch and merges them into another branch in a new commit.When to use it:
Public Branches: For branches that are shared and where others are collaborating, like
masterormain, it's safer to usemergebecause it doesn't rewrite commit history.Merge Commits: If you want to preserve the history of branch merges explicitly with merge commits.
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:
Linear History: If you prefer a clean, linear commit history without merge commits.
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.
Local Cleanup: If you want to clean up or squash your local commits before pushing to a shared repository.
Avoiding Merge Commits: If you don't want the extra merge commits that come from using
git merge.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:
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.
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. Withrebase, you'll address conflicts commit-by-commit as Git replays each change.
- `git commit --amend` to push code changes to a specific commit without creating a new commit



