All the use cases of useState I use in my projects.
Tags: react, beginners, tutorial, javascript
Published: 2020-11-28 17:45:34

In this post I will discuss the ways in which I use <strong>useState</strong> in my projects.



<strong>1. To get API data from backend and store it in state.</strong>
```js
const [data,setdata]=useState([])

useEffect(()=>{
const getdata = async () =>{

const res = await axios.get(apiurl)
setdata([{...res.data}]
}

getdata()

},[])

```
As I use MongoDB most of the time, the data which comes from the backend is in <strong>object/JSON</strong> form.

So to send the data from 
- The frontend in forms and 
- To get the data from the backend, 

I initialize a null or an empty state and get that backend data or the frontend data and push it to the useState variable and display it with map function.

I use <strong>map function</strong> because I initialized an empty array and inserted object data into it as map function works only for arrays.

<strong>Spread operator [...]</strong> is used here because when we want to get data from an API we will not require all the data present inn it and want only a select few.

So to persist the existing data in the array we use spread operator.

I have added spread operator here just to show there is an use case for it in useState as well.

<strong>2. For True or false values.</strong>
```js
const[open,setopen] = useState(false)

{{open && <div>If I Click a button or any other condition I wish or have show this div </div>}}

```
Literally the most used use case of <strong>useState</strong> in workflow.

Whenever the logic permits to have a boolean condition such as show something when I click a button, I just plugin this condition to get the job done!

<strong>3. Regular State management and to pass as props</strong>

```js

const [data,setdata] = useState('2')

<Component data={data}/>
```
This one is the most basic one which all of React developers use, but just to aid beginners I have mentioned it here.

Like the first use case I mentioned above, I can just pass data into an empty array and pass it as props to another component and use my data there.

This will be very useful if you want to create a card like component with map function and output it in the frontend which I do all the time.

And that's some of my most used useState use cases I have mentioned here.

There are a lot more ones which more experienced senior react.js developers may use which I do not know.

If you know any other cases than the mentioned ones here, please mention it in the comments.

### Thank you for reading!!

<strong> <u>Check out my portfolio: </u> [Gautham's portfolio](https://gautham-portfolio.netlify.app/)</strong>

<strong> <u> Check out my blog: </u>[coding-magnified.tech](https://coding-magnified.tech)</strong>


### <u>My Other Articles: </u>

- [Universal CSS properties everyone must know](https://dev.to/gautham495/universal-css-properties-everyone-must-use-4kie)

- [Create-react-app](https://dev.to/gautham495/creating-a-new-react-app-with-create-react-app-4e9m)

- [Git for beginners](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p)

- [Change headers in react with react-helmet](https://dev.to/gautham495/changing-headers-in-react-with-react-helmet-d0d)

- [Know How to apply box-shadow on all four sides.](https://dev.to/gautham495/know-how-to-apply-box-shadow-on-all-four-sides-43pn)
 
- [Simple CSS Selectors](https://dev.to/gautham495/simple-css-selectors-for-beginners-2i44).

- [CSS Pseudo Elements](https://dev.to/gautham495/css-pseudo-elements-for-beginners-53ob).