DEV Community

Cover image for Decoupling Hoisting In React
Oyerinde Samuel
Oyerinde Samuel

Posted on

Decoupling Hoisting In React

**Table Of Content

  1. Brief overview of hoisting in JavaScript
  2. Explanation of how hoisting works in ReactJS
  3. Hoisting in ReactJS
  4. Benefits of using hoisting in ReactJS
  5. Examples of hoisting in ReactJS
  6. Best Practices for Hoisting in ReactJS
  7. Tips for optimizing performance with hoisting in ReactJS
  8. Conclusion
  9. Reference**

Brief overview of hoisting in JavaScript
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their respective scope before the code is executed. This means that you can use a variable or function before it has been declared in your code. However, only the declaration is hoisted, not the initialization, which means that if you try to access a variable before it has been assigned a value, it will be undefined. Hoisting can be a powerful tool, but it can also lead to unexpected results if not used properly.

Variable declarations are hoisted to the top of their respective function or global scope.

a. For example, consider the following code
Javascript code

Even though the console.log(x) statement appears before the declaration of the x variable, the code still runs without error because the declaration is hoisted to the top of the function scope.
b. Function declarations are also hoisted to the top of their respective function or global scope. For example, consider the following code:

Image 2
Even though the example() function is called before it is declared, the code still runs without error because the function declaration is hoisted to the top of the global scope.

Note: It's important to note that while variable and function declarations are hoisted, any assignments or initializations are not hoisted.
For example, consider the following code:

Var hoisting

In this code, the declaration of the x variable is hoisted to the top of the global scope, but the initialization of x is not hoisted. As a result, the console.log(x) statement will output undefined because x has not yet been assigned a value.

Explanation of how Hoisting works in React
In ReactJS, hoisting works in a similar way to how it works in regular JavaScript. React components are defined as functions, and just like any other JavaScript function, the function declaration is hoisted to the top of its scope. This means that you can use the component before it has been defined in your code.

Hoisting in React
For example, consider the following React code:

//1. Hoisting a function component
renderMyComponent();

function renderMyComponent() {
  ReactDOM.render(<MyComponent />, document.getElementById('root'));
}

function MyComponent() {
  return <h1>Hello, world!</h1>;
}

Enter fullscreen mode Exit fullscreen mode

In this example, the MyComponent function component is declared after the renderMyComponent function. However, because the function declaration is hoisted to the top of the scope,
the renderMyComponent function can still call the MyComponent component and render it to the DOM.

Benefits of using hoisting in ReactJS

a. Improved code organization: Hoisting allows you to declare variables and functions at the top of a code block, which can make your code more organized and easier to read.

b. Faster execution: Hoisting can improve the performance of your code by allowing the JavaScript engine to pre-process function and variable declarations before executing the rest of the code.

c. Avoiding errors: Hoisting can prevent errors caused by using variables or functions before they are declared, which can be especially helpful in large ReactJS projects.

Example of Hoisting In React

// 2. Hoisting a state variable
import {useState} from 'react';

const MyComponent = () => {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    setCount(count + 1);
  }

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

In this example, the count state variable is declared and initialized using the useState hook. Even though count is declared after the handleClick function that uses it,
the function can still access count because the declaration is hoisted to the top of the component function scope.

import { useState, useEffect } from "react";
const MyComponent = () => {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    setCount(count + 1);
  }

  useEffect(() => {
    // do some side effect
  }, []);

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

In this example, the handleClick function is declared below the useState hook, but it can still be called in the JSX element because of hoisting. This improves the organization and readability of the code because related code is grouped together.

Similarly, the useState hook is declared above the handleClick function, but it can still be used in the function because of hoisting.
This allows for more flexible placement of code
and can improve performance by allowing the JavaScript engine to pre-process the hook before executing the rest of the code.

Best practices for Hoisting in React
a. Declare state hooks and other hooks at the top of the component, before any other logic or functions:

import { useState, useEffect } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // do some side effect
  }, []);

  const handleClick = () => {
    setCount(count + 1);
  }

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

b. Declare variables and functions before using them:

import { useState } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  // Good: Declare handleClick before using it in JSX
  const handleClick = () => {
    setCount(count + 1);
  }

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

c. Declare functions that use hooks below the hooks they depend on

import { useState, useEffect } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // do some side effect
  }, []);

  const handleClick = ()  {
    setCount(count + 1);
  }

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

d. Avoid declaring functions and hooks inside other functions, as this can make it harder to reason about the code

import { useState, useEffect } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    const innerFunction = () => {
      setCount(count + 1); // This won't work as expected
    }

    innerFunction();
  }

  useEffect(() => {
    // do some side effect
  }, []);

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

Tips for optimizing performance with hoisting in ReactJS

Use hoisting with caution and only when necessary to avoid overcomplicating your code.

Be mindful of the order in which your code is executed to avoid unexpected behavior.

Use profiling tools to identify performance issues and optimize your code accordingly.

Conclusion

Hoisting is an important concept in ReactJS that can improve code organization and performance when used properly. It is important to understand the best practices and pitfalls of hoisting to avoid errors and optimize code performance.

Reference
https://facebook.github.io/react/docs/thinking-in-react.html
https://facebook.github.io/react/docs/lifting-state-up.html
https://www.digitalocean.com/community/tutorials/understanding-hoisting-in-javascript
https://react.dev/blog/2023/03/16/introducing-react-dev

Top comments (0)