Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Add hooks usage guide page #1575

Open
markerikson opened this issue Apr 28, 2020 · 3 comments
Open

Docs: Add hooks usage guide page #1575

markerikson opened this issue Apr 28, 2020 · 3 comments
Labels

Comments

@markerikson
Copy link
Contributor

The usage guide pages for mapState and mapDispatch are amazingly good, and I frequently link them as references to answer people's questions.

However, we currently only have an API reference page for our hooks API. It's time we added some kind of a usage guide page for the hooks as well.

I'm not exactly sure what topics it should cover, but I'd like to hear suggestions.

@AnitaErnszt
Copy link

Hi,
I've been trying to use useDispatch hook within actions.js. Basically I'm having the same actions called (LoadItem, LoadItemSuccess || LoadItemFailure) in multiple components. If I call the api in every single component it works fine, but ideally I would package it into a function like FetchItem that dispatch all the functions and calls the api, and I could reuse FetchItem from the actions in every single component I need it.
This is possible using redux without the hooks, but because hooks can only be called within a react function component I'm not sure this can be done with hooks.
It would be great to see some complex example of the usage of these hooks.

@clifflo
Copy link

clifflo commented Jan 25, 2021

For the documentation of React Redux I have found that it does not state quite clearly how to useDispatch in useEffect(). When we call useDispatch in useEffect there would be error saying that you cannot use hooks within useEffect(). To cope with this problem you will need to add the dispatch object to the array after useEffect(), like this:

let dispatch = useDispatch();
useEffect(() => {
}, [dispatch])

I think this should be stated in the React Redux documentation more clearly.

@markerikson
Copy link
Contributor Author

@clifflo I don't think anything special has to be said there. The React rules of hooks clearly state all hooks must be called at the top level of a function component. That applies to useDispatch as well.

So, yes, the hooks lint rule then says you have to add dispatch as a dependency, because it assumes that all variables that aren't React hooks setter functions could change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants