Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between storing a const value in a variable vs in state?

I've noticed a couple of ways of achieving seemingly the same thing inside a React functional component. When you have what is essentially a config value that is only needed inside this component (Just a constant value, never passed in or modified) You could just use a regular const or you could store it in the component's state.

Standard variable:

function Example1() {
  const a = 1
  return <div>{ a }</div>
}

Stored in state:

function Example2() {
  const [a] = useState(1)
  return <div>{ a }</div>
}

I get the impression that behind the scenes this would lead to Example1 creating a variable on each render and then disposing of it, while Example2 will create the variable once and maintain it until the component is unloaded. Is that accurate? And is one of these methods preferable in terms of performance/good practice?

like image 856
DBS Avatar asked Mar 23 '20 14:03

DBS


People also ask

What will happen if you put some value inside a variable instead of state in React?

Variables by themselves only change bits in memory and the state of your app can get out of sync with the view. In both cases a changes on click but only when you use useState the view correctly shows a 's current value. Thanks!

When would you use state instead of props?

Use state to store the data your current page needs in your controller-view. Use props to pass data & event handlers down to your child components. These lists should help guide you when working with data in your components.

What are the significant differences between state and props?

Props are used to pass data from one component to another. The state is a local data storage that is local to the component only and cannot be passed to other components. The this. setState property is used to update the state values in the component.

What is the difference between state and setState?

State can be updated in response to event handlers, server responses, or prop changes. This is done using the setState() method. The setState() method enqueues all of the updates made to the component state and instructs React to re-render the component and its children with the updated state.

What is the difference between a constant and a variable?

The constant is similar to a variable, but it cannot be modified by the program once it is defined. whereas the variable is a container or a storage area to hold data. A constant cannot be changed by the program once it is defined. A variable can be changed by the program once it is defined.

Should you store data in state or class variables?

However, if you’re going to render that data, you should instead store it in state so that any changes trigger a re-render. Ninety-five percent of the time, you’ll store data in state. Now that you know the difference between storing data in state and class variables, you can be more helpful than I was to your fellow developers.

What is the difference between static and const in C++?

But in const that is for only one value where as in static values may change but the memory area remains the same until the end of the program. static keyword defines the scope of variables whereas const keyword defines the value of variable that can't be changed during program execution Const means “cannot be changed.”

What are the different data types of constants?

The constants can be different data types, such as integer constants, floating constants, character constants, string constants and enumeration constants. Let's understand briefly about them: An integer constant is a whole number and can be large without including any decimal points.


1 Answers

Is that accurate?

Yes, as you said, Example1 creates a variable on each render (marking it as "disposable" in the end of the scope - not related to React but Javascript), Example2 creates the variable once and maintains it until the component is unmounted (or the state of this variable changes via setState).

And is one of these methods preferable in terms of performance/good practice?

As a good practice - Example1.

As for performance, it should be Example1. Example2 runs useState and compares the value a with the previous state on every render which is "much more expensive" than declaring a variable.

A better example will be comparing component reference/memoized variable vs. variable (Example1):

function Example2() {
  const a = useRef(1);
  const b = useMemo(() => 1, []);
  return <div>{a.current} , {b}</div>
}

But the answer remains almost the same.

Seeing such code indicates that the ref a might change. The use of useMemo indicates that b is a "heavy computation" variable, if not, it's just an overhead (same explanation as above) and better use Example 1.

like image 161
Dennis Vash Avatar answered Nov 03 '22 00:11

Dennis Vash