Syntax - Tasty Web Development Treats
Syntax - Tasty Web Development Treats

React Hooks

In this episode Wes and Scott discuss React Hooks - what they are, why you might want to use them, their differences with regular classes, and more. Freshbooks - Sponsor Get a 30 day free trial of Freshbooks at freshbooks.com/syntax and put SYNTAX in the “How did you hear about us?” section. Sentry

Featured Speakers

Wes Bos & Scott Tolinski - Full Stack JavaScript Web Developers Host

Topics Discussed

Episode Summary

Executive Summary: The hosts introduce React Hooks as an opt-in way to use state, context, effects, reducers, and refs in function components, reducing reliance on class components and render-prop or higher-order-component patterns. They explain key hooks, emphasize that existing code need not be rewritten immediately, and recommend waiting for real-world patterns and library support to mature.

Main Topics: Why React Hooks were introduced: Function components previously could not use state, context, or lifecycle behavior, so developers often converted them to classes or composed logic through mixins, higher-order components, and render props. Hooks offer another way to share behavior while reducing component-tree and debugging clutter. State with useState: useState initializes a state value and returns an array containing that value and its updater, which can be destructured into developer-chosen names. Components can use multiple independent state hooks; unlike class setState, updating an object-valued state does not automatically merge its properties. Context and useContext: useContext reads the current value from a context object without nesting a render-prop consumer in the component's JSX. A provider is still needed, but consuming shared values and functions becomes more direct and potentially easier to teach. Effects and reusable logic: useEffect handles side effects such as API work, subscriptions, DOM interactions, and third-party integrations. Its function runs after mounting and updates, and a returned function provides cleanup. The hosts highlight that custom hooks can package and share such behavior. Reducers, refs, and additional hooks: The reducer hook supports state updates organized around actions and reducer functions. The ref hook provides access to DOM elements for tasks such as focusing inputs. The discussion also mentions specialized effect and imperative hooks, while acknowledging that some details and use cases were still emerging. Adoption and early ecosystem: Hooks were presented as experimental and opt-in at the time of recording. The hosts advise against rushing a full production rewrite and recommend observing community practices. They point to early hook-enabled libraries, including React Spring and Formik, and resources such as usehooks.com and the Awesome React Hooks list.

Key Arguments: Hooks address a practical limitation of function components: adding state or lifecycle-related behavior previously required conversion to a class or composition patterns that could make code and component trees harder to navigate. Hooks are an incremental option, not a mandate. Existing class components and render-prop patterns can remain, and teams should adopt hooks when they solve a real problem rather than rewrite an entire application preemptively. Separating state into individual useState calls can make component logic clearer, but object updates require deliberate handling, such as spreading prior state or using an updater callback. useEffect and custom hooks can group related side-effect logic and cleanup, making behavior reusable and potentially improving readability and reducing bugs. Reducers and context may cover some needs that developers currently address with Redux, but the hosts do not claim Redux is obsolete; they note that development tools and established use cases remain relevant. The long-term value of hooks depends on experience from real applications and mature library APIs; community experimentation is expected to establish best practices.

Data Points: React version mentioned for trying hooks: React 16.7 alpha - The hosts say hooks were not yet in a stable React release at recording time and required installing an alpha version. Sentry error occurrences: Almost 20,000 times - In a sponsor anecdote, a front-end error involving scrolling a video list had occurred nearly 20,000 times. Users affected by the Sentry error: 7.5 thousand - The same error was reported as affecting roughly 7,500 users. Sentry resolution-time claim: Five hours to five minutes - The Sentry sponsor segment cites a claim that Sentry 9 reduced resolution time from five hours to five minutes. Other browser share in the error report: 1% Opera - The host says the reported issue appeared in Chrome and, at a 1% share, Opera, which uses the same engine.

Pivotal Quotes: "hooks are 100% opt-in and classes aren't going anywhere." — Host: The hosts reassure listeners that hooks do not require immediate changes to existing applications. "I probably wouldn't go out and rewrite all of your components unless you say, Yes, I have that problem and this is going to fix it." — Host: Practical advice to adopt hooks in response to a concrete need rather than chasing a new API. "the most exciting part is the fact that we can store these as functions and share them" — Host: The discussion emphasizes reusable custom hooks as a major opportunity for libraries and application code.

Implications: For developers, hooks offer a cleaner route to reusable stateful behavior in function components, but adoption should be incremental. Library authors and teams can experiment, while production users should allow APIs and community conventions to mature before undertaking broad migrations.

🔓 Sign Up for Unlimited Episode Search

About Syntax - Tasty Web Development Treats

View all episodes from Syntax - Tasty Web Development Treats