What is the useState hook in React? | Tekolio

Index

What is useState() hook?

How to declare it?

import React, { useState } from "react";

Syntax

import React, { useState } from 'react'; 
function Example() {
const [count, setCount] = useState(initial value);
}

Rules for using useState() hook

Only call hooks at the top

Only call hooks in React Functions

Example time

import React from "react";const Count = () => {const [count, setCount] = useState(0);return (<><p>You clicked {count} times</p><button onClick={() => setCount(count + 1)}>10: Click me 11: </button></>);};export default Count;

Conclusion

--

--

--

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ateev Duggal

Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.

More from Medium

Fetching Data from an API in Vanilla Javascript…Star Wars Style

12 tips to help you debug your react application

REACT ROUTERS

Opus Magnum: a Solid Foundation for Understanding: 1) ReactJS Routing; 2) Responsive Routing (what?)

URL adrress parts