Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why React Hook useState uses const and not let

The standard way to use a React useState Hook is the following:

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

However this const count variable is clearly going to be reassigned to a different primitive value.

Why then is the variable not defined as let count?

like image 223
Nacho Avatar asked Nov 14 '19 15:11

Nacho


People also ask

Why React Hooks are use full?

Hooks make React so much better because you have simpler code that implements similar functionalities faster and more effectively. You can also implement React state and lifecycle methods without writing classes.

How does useState Hook work in React?

React calls your component for the first time. React finds a call to useState , creates a new Hook object (with the initial state), changes the current Hook variable to point to this object, adds the object to the Hooks list, and return the array with the initial state and the function to update it.

Is useState Hook synchronous or asynchronous?

TL;DR: useState is an asynchronous hook and it doesn't change the state immediately, it has to wait for the component to re-render. useRef is a synchronous hook that updates the state immediately and persists its value through the component's lifecycle, but it doesn't trigger a re-render.

Why does the React useState Hook not update immediately?

The answer: They're just queues setState , and React. useState create queues for React core to update the state object of a React component. So the process to update React state is asynchronous for performance reasons. That's why changes don't feel immediate.

What is usestate hook in react?

useState is a React hook for managing components rendering. The hook can be used inside each component which needs to be updated and re-rendered according to its state changes. useState hook is a method that accepts just one argument which is an initial state. A function that returns initial state (using for a more complex way of its defining).

How do react hooks work with functional components?

In React Hooks with a Functional Component, your code gets a single value of state for each call into your functional component. React handles the storage separately and returns that current value via useState on each execution of your code, providing the latest state value.

What is going on with the const keyword in react hooks?

Normally, const is used on variables that won't be changing. Here, someBooleanValue will be changing. What is going on that allows us to use the const keyword in this case? Show activity on this post. In React Hooks with a Functional Component, your code gets a single value of state for each call into your functional component.

What is the difference between usestate () and const () in react?

It's not exactly assigning a new value. useState is simply a state updating function. Const is used here because the change of value is being managed somewhere else by React. You're telling React to manage some value for you by calling useState.


2 Answers

clearly going to be reassigned to a different primitive value

Not really. When the component is rerendered, the function is executed again, creating a new scope, creating a new count variable, which has nothing to do with the previous variable.

Example:

let _state;  let _initialized = false;  function useState(initialValue) {    if (!_initialized) {      _state = initialValue;      _initialized = true;    }    return [_state, v => _state = v];  }    function Component() {    const [count, setCount] = useState(0);      console.log(count);    setCount(count + 1);  }    Component();  Component(); // in reality `setCount` somehow triggers a rerender, calling Component again  Component(); // another rerender

Note: Hooks are way more sophisticated and are not actually implemented like this. This is just to demonstrate a similar behavior.

like image 173
Felix Kling Avatar answered Oct 01 '22 01:10

Felix Kling


const is a guard against reassigning the value of the reference within the same scope.

From MDN

It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

Also

A constant cannot share its name with a function or a variable in the same scope.

like image 43
tony Avatar answered Oct 01 '22 03:10

tony