Is it bad practice to use normal variables in React components?
P粉647504283
P粉647504283 2023-08-16 16:02:13
0
1
459
<p>In my React project, I need a variable that depends on two state variables. Do I need to use useState for this variable, or only for the variables it depends on? </p> <p>For example, is this design pattern possible:</p> <pre class="brush:js;toolbar:false;">const [number1, setNumber1] = useState(2); const [number2, setNumber2] = useState(2); const sum = number1 number2; </pre> <p>Or do I need to create sum as state and update it with useState when number1 or number2 changes (e.g. in useEffect callback)? </p>
P粉647504283
P粉647504283

reply all(1)
P粉378890106

This is not just "acceptable", it's exactly what you should do!

In fact, using additional states and effects to make changes would be considered inefficient and unnecessary: ​​

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!