I have few scenarios and I'd like to understand the differences in regards to rendering and performance.
The example shown below is for a simple function but please consider a more complex one too as well as an async function.
Scenario 1: Defining function and calling it inside useEffect.
useEffect(() => {
function getBooks() {
console.log('I get books!');
}
getBooks();
}, []);
Scenario 2: Defining function outside UseEffect and calling it inside of it.
function getBooks() {
console.log('I get books!');
}
useEffect(() => {
getBooks();
}, []);
Scenario 3: Using useCallback to define the function outside of UseEffect and calling it inside of it.
const getBooks = useCallback(() => {
console.log('I get books!');
}, []);
useEffect(() => {
getBooks();
}, []);
Scenario 4: Using useCallback to define a function inside UseEffect and calling it also inside UseEffect.
useEffect(() => {
const getBooks = useCallback(() => {
console.log('I get books!');
}, []);
getBooks();
}, []);