React useState's usage
Hello everyvone, I will tell you useState hook's using on the React. React creates user interface design so, a lot of subject also helpful developer. Our website visited users, useState users' catch interaction and it will be render components again. In addition to this what is Components? On our website cerated all React elements are a component.
For example, create a component; in the field I have selected or called, the component will write "Hello World!"
const textVisible = () => {
return <p>Hello World!</p>
}
Okay, let's understand the general work logic of Components and useState and see the syntax of useState. First, start with importing the useState hook.
import { useState } from 'react';
In addition to this, create a component that takes an empty parameter.
import React, { useState } from 'react';
export default function App() {
return (
<div>
<h1>Hello World!</h1>
</div>
);
}
And now, firstly create the useState hook and the useState hook takes the following parameters:
const [state, setState] = useState(initialState);
Let's using useState hook and For example;
import { useState } from "react";
import "./styles.css";
export default function App() {
const [number, setNumber] = useState(0);
return (
<div className="App">
<p>{number}</p>
<button onClick={() => setNumber(number + 1)}>Increase</button>
</div>
);
}
However look at writed code, created a button and "<p>" label with text and every button clicked time state variable's value one increase code working but "<button>" label in use a problem exist,
On click number via variable one increase and that isn't good practices. Instead of this that "setNumber" function let's change like the following.
<button onClick={() => setNumber((p) => p + 1}>Increase</button>
With that code, the value of the number variable increases by one and it becomes suitable for use.
Recommended by LinkedIn
Now, let's create a difficult example;
And now, let's proceed with step-by-step code writing.
const [users, setUsers] = useState([]);
const [name, setName] = useState("");
const [lastName, setLastName] = useState("");
<form onSubmit={(e) => handleSubmit(e)}>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Entry name"
/>
<input
value={lastName}
onChange={(e) => setLastName(e.target.value)}
placeholder="Entry surname"
/>
<button>Add a new user</button>
</form>
Okay, now created code in different structure available and let's analysing code.
const handleSubmit = (e) => {
e.preventDefault();
if (name && lastName) {
setUsers([...users, { id: crypto.randomUUID(), name, lastName }]);
setName("");
setLastName("");
} else if (!name || !lastName) {
alert("Form has empty values!");
}
};
"handleSubmit" function details;
At last, updated users list values with over selected HTML elements will add.
{users.length > 0 ? (
users.map((item) => {
return <p key={item.id}>{`${item.name} ${item.lastName}`}</p>;
})
) : (
<p>Users list are empty</p>
)}
With this article React useState hook's use to tell tried me and React over official document detailed information can take.
Thank you for recommending our website! Your support means a lot to us! 💚