React | Advanced Problems & Solutions πŸ˜‰

Shahzaib Khalid

These slides are available at (https://www.shahzaibkhalid.com/react-advanced-issues-and-solutions)

Stop! πŸ›‘

This is not an introduction to React! 😢

You must have an idea about:

  • Props
  • State
  • Lifecycle Methods

Who am I? 😁

Shahzaib's Avatar

A front-end developer πŸ‘¨πŸ»β€πŸ’» mainly interested in JavaScript & React. βš›οΈŽ

@shahzaibkhalid | shahzaibkhalid.com

React is Winning! πŸŽ‰

npm trends about top JS frameworks

But...

There are some pain-points! 😭

What this talk is about

  • Issues with classes in React 😒
  • How does React Hooks solve problems with classes? πŸ€”
  • Pain of sharing code among components using HOC & Render Props πŸ˜’
  • How does React Hooks simplify code sharing among components? πŸ€”

Issues with classes in React

Let's dive in! πŸ‘‰
Class Component
Functional Component

We need to use classes to use state and lifecycle methods

Problem: 01

Classes has annoying behavior when it comes to "this" 😒

Problem: 02

Related logic is split across Lifecycle methods 😐

Logic split across component
Problem: 03

You're not sure when you'd be converting functional component to class πŸ€”

Problem: 04

Classes are hard to minify and tree-shake 😢

Classes are hard to minify and tree-shake
Problem: 05

Classes makes it difficult to hot-reload your code 😟

How we can solve these problems with Classes? πŸ€”

By avoiding the Classes! Simple. πŸ˜…

React Hooks πŸŽ‰

Use State and Lifecycle, straight from simple JavaScript functions!

Hook: 01

useState

It let us hook state into functional components! πŸ˜‰

Let's compare Classes with Hooks πŸ”₯

Class Component
Functional Component

But, what to do if I've a state object, instead of a single value?

Hook: 02

useReducer

Inspired by Redux, it let us hook state object into functional component! πŸ˜‰

Let's compare Classes vs Hooks for useReducer!

Hook: 03

useRef

Built-in React Hook to access any JSX Element πŸ––

Already covered! 😎

There are many more built-in Hooks! πŸ™

But for now, let's discuss problem other than Classes

Sharing stateful logic among React Components 😢

Sharing stateful logic and sharing code are two different things. ☘️

How?

Code sharing is different

Let's understand through an example that what's the meaning of sharing stateful logic?

Content Normal
Sidebar Normal

HOC (Higher Order Component) 🀟🏼

withJoke HOC
Content HOC
Sidebar HOC

But, HOCs can lead us to "Wrapper Hell" πŸ‘€

HOC Hell

Render Props to Rescue πŸš‘

Render Prop Joke
Content Render Prop
Sidebar Render Prop

Multiple Render-Props can lead us to "massive nesting" 😢

Render Prop Nesting

(Credits: Nick Gard - Why I Don't Use Render Props in React on Medium)

Custom Hooks πŸŽ‰

Custom useJoke Hook
Custom Hook Content
Custom Hook Sidebar

Custom Hooks are the most exciting features of Hooks! πŸ˜‰

No nesting & wrapper hell!

Rules of Hooks!

  • Only Call Hooks at the Top Level
  • Custom Hooks names should always start with use
  • Don’t call Hooks from regular JavaScript functions. Instead, you can:

    • Call Hooks from React function components.
    • Call Hooks from custom Hooks

And there are many more Hooks to checkout!

Like, useDarkMode & useMemo 😎

  • For React built-in Hooks, checkout Hooks API Reference
  • For community built custom hooks, checkout useHooks.com

These slides are built with React too. 😎

Checkout the source code at: @shahzaibkhalid/react-advanced-issues-and-solutions