Quick Guide on React useState() Hook

Search for a command to run...

No comments yet. Be the first to comment.
š” āSo if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.ā -By Robert C. Martin JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you ...

Before beginning our quest to find how not to use Create React App. First, letās see why we need Create React App in the first place. Why do we need Create React App? š¤ Create React App is a comfortable environment for learning React and is the best...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure? Canāt I just stuff all my files in the src folder?? 𤪠Technically you could ...

Letās first understand š¤ What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...

In React, Hooks are in-built functions that allow React Developers to use React State and lifecycle methods inside the functional components.
When you take an initial look at the React Hooks documentation, youāll see that there are several Hooks that we can use for our applications. You can even create your own custom hook.
In this Section, we basically deal with the useState hook:-
Before going further let's see Some advantages of React Hooks:-
Letās start from Scratch
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
// Create function component:
function App() {
// Declare new state without initial value:
const [count, setCount] = useState()
// Declare new state with string as initial value:
const [word, setWord] = useState('Hello!')
// Declare new state with number as initial value:
const [num, setNum] = useState(0)
// Declare new state with array as initial value:
const [series, setSeries] = useState([0, 1, 2, 3])
// Declare new state with object as initial value:
const [person, setPerson] = useState({
name: 'pankaj',
email: 'pankaj@gmail.com'
})
return (
<div>
{/* ... */}
</div>
)
}
The names of state variables above can be anything you want, it doesn't matter for React. I personally advise using meaningful variable names depending on the state's purpose.
Now, could we use the initial value of state ??
see the example below
// Create function component:
function App() {
// Declare states for name and age:
const [name, setName] = useState({
firstName: 'Pankaj',
lastName: 'Kumar'
})
const [age, setAge] = useState(21)
return (
<div>
{/* Read from the "name" state. */}
<p>Hello, my name is: {name.firstName} {name.lastName}</p>
{/* Read from the "age" state. */}
<p>My age is: {age}</p>
</div>
)
}
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
function App() {
//declare count state
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}