gg

Understanding React Auth & Hooks

🛠 React: useState vs useRef vs useMemo + Login Explained

1️⃣ useState vs useRef

useState is for updating and re-rendering the component. useRef stores a value across renders without causing a re-render.


const [count, setCount] = useState(0);
const countRef = useRef(0);

2️⃣ useRef vs useMemo

useRef stores values or DOM nodes. useMemo is for performance, caching results of calculations.


const value = useMemo(() => expensiveCalc(num), [num]);

3️⃣ How Login Works in React

  1. User fills login form
  2. React sends data to backend
  3. Backend validates and sends token
  4. React stores token
  5. Future requests include token

4️⃣ Where Token is Stored

Tokens are stored in:

  • localStorage
  • sessionStorage
  • Cookies (more secure)

5️⃣ Who Generates the Token?

The backend (e.g. Express, Django) generates the token and returns it to React. React stores and uses it for authentication.

Comments

Popular posts from this blog

React Interview Questions - Controlled vs Uncontrolled

What is the closure function in Javascrpt(javascrpt)