An arrow function is a shorter way to write a function in JavaScript.
It uses the => symbol instead of the function keyword.
Basic Syntax
const add = (a, b) => {
return a + b;
};
You can also write it even shorter if it's just one line:
const add = (a, b) => a + b;
Example:
Instead of writing:
function sayHello() {
console.log("Hello");
}
You can write:
const sayHello = () => {
console.log("Hello");
};
Key Points:
- It uses
=>instead of thefunctionkeyword. - It's shorter and cleaner.
- It automatically uses the
thisfrom the place where it was written.
Callback Function:
A callback function in JavaScript is a function that is passed as an argument to another function, and it is called (or "called back") later.
Example:
function greet(name, callback) {
console.log("Hello, " + name);
callback();
}
function sayBye() {
console.log("Goodbye!");
}
greet("John", sayBye);
Output:
Hello, John
Goodbye!
Here:
-
sayByeis a callback passed togreet. -
greetcallssayBye()after greeting.
In Short:
A callback is:
- A function passed into another function
- Called later (after something happens)
- Useful for things like waiting, repeating, or responding to events
UseEffect:
useEffect is a React Hook that lets you run side effects in function components.
useEffectruns after your component renders. You use it to do things like fetch data, update the DOM, or set up timers.
Basic Syntax:
import { useEffect } from 'react';
useEffect(() => {
// Code to run (side effect)
});
Important Notes:
-
useEffectruns after render. - You can control when it runs using the dependency array (
[]). - You can return a function to do cleanup (like clearing timers or unsubscribing from listeners).
Top comments (0)