![]() ![]() For a larger app, or one that you intend to scale up, you’ll probably want to augment useState with some other state management solutions. The useState hook is perfect for local component state, and a small-ish amount of it. Each call to useState creates a single piece of state, holding a single value of any type. In class components, the state was always an object, and you could store multiple values in that object.īut with hooks, the state can be any type you want – you can useState with an array, useState an object, a number, a boolean, a string, whatever you need. (every hook starts with the word “use” a call to useState literally lets you “use state” in a function component) (Not sure what a “hook” is? Read this intro to hooks!)īy calling eState inside a function component, you create a single piece of state associated with that component. The useState hook lets you add state to function components. We’ll also see a couple ways of storing multiple values.įYI if you’re brand new to React, I suggest pausing here and reading my React tutorial first – it covers all the important basics – and then come back here for the more advanced uses of useState. Here we’ll cover useState with simple types, as well as useState with objects and arrays. There are a bunch of React hooks, but useState is the workhorse. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |