Lecture No 1 React Hook useState() How its work and used. #reactjs #javascript #development

1 year ago
10

The useState() is a Hook that allows you to have state variables in functional components. so basically useState is the ability to encapsulate local state in a functional component. React has two types of components, one is class components which are ES6 classes that extend from React and the other is functional components. Class components a Component and can have state and lifecycle methods: class Message extends React. The useState hook is a special function that takes the initial state as an argument and returns an array of two entries. UseState encapsulates only a singular value from the state, for multiple states need to have useState calls.

Syntax: The first element is the initial state and the second one is a function that is used for updating the state.

const [state, setState] = useState(initialstate)
We can also pass a function as an argument if the initial state has to be computed. The value returned by the function will be used as the initial state.

const [sum, setsum] = useState(function generateRandomInteger(){5+7);})
The above function is an online function that computes the sum of two numbers and will be set as the initial state.

#javascript #reactjs #map #developer #development #hooks #javascriptengineer

Loading comments...