What is useState() in React? What is useState() in React? reactjs reactjs

What is useState() in React?


React hooks are a new way (still being developed) to access the core features of react such as state without having to use classes, in your example if you want to increment a counter directly in the handler function without specifying it directly in the onClick prop, you could do something like:

...const [count, setCounter] = useState(0);const [moreStuff, setMoreStuff] = useState(...);...const setCount = () => {    setCounter(count + 1);    setMoreStuff(...);    ...};

and onClick:

<button onClick={setCount}>    Click me</button>

Let's quickly explain what is going on in this line:

const [count, setCounter] = useState(0);

useState(0) returns a tuple where the first parameter count is the current state of the counter and setCounter is the method that will allow us to update the counter's state. We can use the setCounter method to update the state of count anywhere - In this case we are using it inside of the setCount function where we can do more things; the idea with hooks is that we are able to keep our code more functional and avoid class based components if not desired/needed.

I wrote a complete article about hooks with multiple examples (including counters) such as this codepen, I made use of useState, useEffect, useContext, and custom hooks. I could get into more details about how hooks work on this answer but the documentation does a very good job explaining the state hook and other hooks in detail, hope it helps.

update: Hooks are not longer a proposal, since version 16.8 they're now available to be used, there is a section in React's site that answers some of the FAQ.


useState is one of build-in react hooks available in 0.16.7 version.

useState should be used only inside functional components. useState is the way if we need an internal state and don't need to implement more complex logic such as lifecycle methods.

const [state, setState] = useState(initialState);

Returns a stateful value, and a function to update it.

During the initial render, the returned state (state) is the same as the value passed as the first argument (initialState).

The setState function is used to update the state. It accepts a new state value and enqueues a re-render of the component.

Please note that useState hook callback for updating the state behaves differently than components this.setState. To show you the difference I prepared two examples.

class UserInfoClass extends React.Component {  state = { firstName: 'John', lastName: 'Doe' };    render() {    return <div>      <p>userInfo: {JSON.stringify(this.state)}</p>      <button onClick={() => this.setState({         firstName: 'Jason'      })}>Update name to Jason</button>    </div>;  }}// Please note that new object is created when setUserInfo callback is usedfunction UserInfoFunction() {  const [userInfo, setUserInfo] = React.useState({     firstName: 'John', lastName: 'Doe',  });  return (    <div>      <p>userInfo: {JSON.stringify(userInfo)}</p>      <button onClick={() => setUserInfo({ firstName: 'Jason' })}>Update name to Jason</button>    </div>  );}ReactDOM.render(  <div>    <UserInfoClass />    <UserInfoFunction />  </div>, document.querySelector('#app'));
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script><script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script><div id="app"></div>