
What is better: useState vs manual
In react(18.0.0) if you need a state or to toggle a state you might think Manual seems good for example: let State = 0 function App () { return State = 1 ; } export default App ; This state wont change. Now react has a hook designed for this called useState this allows you to manage states. You can set a string: import { useState } from " react " ; const MyString = useState ( ' Hello, World! ' ); console . log ( MyString ); A boolean: import { useState } from " react " ; const MyBoolean = useState ( false ); console . log ( MyBoolean ); A number: import { useState } from " react " ; const MyNumber = useState ( 0 ); console . log ( MyNumber ); However.. thats still not how you do use state.. if you make a variable using it lets say a number: import { useState } from " react " ; const [ MyNumber , SetMyNumber ] = useState ( 0 ); for numbers that means now you can switch its state: SetMyNumber(5); Comparision table: Manual useState No react hook needed React hook required No rerender Rere
Continue reading on Dev.to Webdev
Opens in a new tab

