Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Introducing React Hooks

Introducing React Hooks

Introducing React Hooks

Avatar for Rizal Ibnu

Rizal Ibnu

May 04, 2019
Tweet

More Decks by Rizal Ibnu

Other Decks in Programming

Transcript

  1. What are Hooks? Hooks are a new addition in React

    16.8. They let you use state and other React features (such as lifecycle) without writing a class.
  2. Problems with writing React Class Component? • How to reuse

    stateful logic • Huge component • Class is confusing (for both humans and machines)
  3. How we deal with them? • Mixins (The old way)

    • Higher-Order Components a.k.a HOC • Render Props
  4. HOC

  5. HOC

  6. HOC

  7. HOC

  8. HOC

  9. HOC

  10. HOC

  11. HOC

  12. HOC

  13. HOC

  14. Rule of Hooks 1. Only Call Hooks at the Top

    Level Don’t call Hooks inside loops, conditions, or nested functions. 2. Only Call Hooks from React Functions Not from regular JS functions or inside class components, but you can call them from custom hooks.
  15. About Hooks • No breaking change • Class still works

    • It will cover all use cases for classes! (except getSnapshotBeforeUpdate and componentDidCatch , but they plan to add them soon)
  16. Hooks API Basic Hooks Additional Hooks useState useEffect useContext useReducer

    useCallback useMemo useRef useImperativeHandle useLayoutEffect useDebugValue https://reactjs.org/docs/hooks-reference.html
  17. How to try Hooks yarn add react@^16.8.0 yarn add react-dom@^16.8.0

    (web) yarn add react-native@^0.59.0 (react native) Recommended yarn add eslint-plugin-react-hooks --dev Code Example https://github.com/rizalibnu/ReactHooksExample (web) https://github.com/rizalibnu/ReactNativeHooksExample (react native)