Skip to content

Hooks API

ALL edited this page Jan 4, 2020 · 3 revisions

Hooks API

  • Let you use state and other React features without writing a class

  • useState is a Hook, we call it inside a function component to add some local state to it

    • useState returns a pair: the current state value and a function that lets you update it
  • The only argument to useState is the initial state

  • Declaring multiple state variables

    `function ExampleWithManyStates() {
     // Declare multiple state variables!
     const [age, setAge] = useState(42);
     const [fruit, setFruit] = useState('banana');
     const [todos, setTodos] = useState([{ text: 'Learn Hooks' }]);
     // ...
     }`
    
  • Hooks are functions that let you “hook into” React state and lifecycle features from function components

  • Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions

  • Only call Hooks from React function components

Additional Resources

Video

Bookmark

Clone this wiki locally