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?
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!
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.
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.
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.
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.
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.
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.”
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With