Docs: Rewrite tutorial to show hooks as the primary API
See original GitHub issueWe’re now at a point where I’m comfortable teaching our hooks API first (which I’m also doing right now as I work on the new “Quick Start” Redux core tutorial page).
Given that, it would be nice to convert the React-Redux tutorial over. Would still be helpful to have the connect
intro somehow as well, albeit secondary.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:33 (15 by maintainers)
Top Results From Across the Web
Mark Erikson on Twitter: "I'd really love to have someone update the ...
Docs : Rewrite tutorial to show hooks as the primary API · Issue #1574 · reduxjs/react-redux. We're now at a point where I'm...
Read more >Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This new...
Read more >Hooks — Wagtail Documentation 4.1.1 documentation
This provides a way to register your own functions to execute at certain points in Wagtail's execution, such as when a page is...
Read more >githooks Documentation - Git
See the documentation for each hook below for details. git init may copy hooks to ... See contrib/hooks/setgitperms.perl for an example of how...
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
I have provided another StackBlitz demo that enhances the original Context API example. Remember, this code below is not using Hooks, we will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m really glad we’re at this point! I think it’s the right direction, especially since reactjs.org itself is undergoing the same transition to hooks-first docs.
I’ve been using hooks and the react-redux hooks since they’ve come out and I have a few opinionated takes I wanted to share. If you agree with any of these, feel free to take inspiration in the docs (otherwise, feel free to ignore 😅)
Click to reveal wall of text
Why hooks first?
Hooks are the React team’s solution to many problems in React. They represent the continued future of React and as stated before, they’re also transitioning the current docs to be hooks-first.
I think it’s important to state why hooks should be the preferred option over
connect
. Here are my reasons (no particular order):Easier to use with TypeScript
This one is just easy to get out of the way. React hooks were built with static typing in mind. Properly adding types to a higher-order component is complex and confusing. Hooks return the values they inject inline, types intact. This holds true for react-redux.
Easier to compose with other pieces of global state
React hooks were built for composition. If you want to join two or more pieces of state, you can do so in the component inline now.
And custom hooks can elevate these into reusable pieces:
This contrasts heavily with some previous best practices back in the day. For example, the purpose of the proejcts
react-router-redux
andconnected-react-router
were to copy the state of react-router into redux state just so it could be used inmapStateToProps
. Given the complexity of higher-order components, this was a very valid pattern but now seems like the wrong place given the ability to compose inline a component or in custom hooks.Easier to understand due to less indirection
useSelector
/useDispatch
are APIs with a lighter footprint. You use them inline, directly in the component.If you want to grab a particular item from state, you can directly return it from
useSelector
(with the types intact). If you want to dispatch an action, it’s perfectly fine to calldispatch
inlineCompare this with the
connect
version:Works well with the default React hooks
In particular, I’ve found it useful to intentionally not calculate values in
useSelector
and instead rely on built-in optimizations likeuseMemo
andReact.memo
.And
useSelector
/useDispatch
also work well withuseEffect
In general, I like to be as React idiomatic as possible and hooks-first is the way 😎
Anyway, that’s all I got!
aaaand done! just nuked the versioning setup, so you’ll probably want to go ahead and do a fresh pull from
master
over into your fork before you get started. I also moved the legacyconnect
tutorial into/docs/tutorials/connect.md
.