Make React Function Components Stateful with useState

Share this video with your friends

Send Tweet

For function components, the useState hook is how you make your component stateful. This replaces the previous convention from class components where values were stored in a this.state object and manipulated using this.setState().

The hook returns an array with two values: the state value your are storing and a function that allows you to updated it. When instantiating the hook, the initial state value is determined by whatever value you pass it.

const [value, setValue] = useState(null)
Diana
Diana
~ 4 years ago

I'm a little confused about the remove prop. I was under the impression that we shouldn't call hooks inside if statements.

Will Johnson
Will Johnson
~ 4 years ago

I'm a little confused about the remove prop. I was under the impression that we shouldn't call hooks inside if statements.

Hey Diana, according to the Rules of Hooks If we want to run an effect conditionally, we can put that condition inside our Hook