-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
add react hooks for accessing redux store state and dispatching redux actions #1248
Conversation
Deploy preview for react-redux-docs ready! Built with commit 91116ac |
Awesome. Initial thoughts:
Also, not sure whether it's better to merge straight into master and publish the alpha from there, or set up a |
|
||
latestSelectedState.current = newSelectedState | ||
} catch { | ||
// we ignore all errors here, since when the component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My one concern here is swallowing errors when the component doesn't re-render.
Perhaps it's worth considering that "window of opportunity" approach from easy-peasy
again?
The other downside is potentially losing some of the stack trace pointing back to the dispatched action.
I like the idea of only needing to import one hook. Only trade-off I can think of is possible confusion or needless bikeshedding by giving people two very similar ways to do something. Not sure migration would be much more difficult by not including it.
I didn't like it at first, but I actually like that it's more explicit about what the hook is doing. This may be especially helpful for beginners. I was a fan of |
Hi @markerikson,
I've checked Docusaurus' docs regarding versioned docs on other branches, it seems they cannot associate a version to a specific branch. The changes to docs should happen on |
In my POC I also had
I'll add a
Yeah, I had the same thoughts. However, since this hook can easily be written by anyone themselves and in the spirit of keeping the exposed API minimal, I'll remove the export of this hook.
The window of opportunity (as implemented in easy-peasy) would also only throw the error if the component gets re-rendered. I still argue that for transient errors or for unmounted components it simply does not matter. The selector is a pure function without side-effects. Therefore, why would you care about an error that never has any observable effect (quantum scientists would argue that any component is always in an error state as long as you don't render it ;) )?
Yeah, I hadn't thought about that. I have set up a sandbox that shows how it would currently look like. In the example you don't get the dispatch in the stack trace, only the click event that triggered the dispatch. I have prototyped a solution for this here and you can see it in action in this sandbox. Basically, if in a render following an error in the subscription callback another error occurs, we merge the errors. This way you get both stack traces. What do you think? |
Well, the latest poll between just So, I'm inclined to stick with |
There would be some problems with HMR, and probably not only with it.
There is a simple way to mitigate |
@MrWolfZ : I think I was worried that there might still be a case where an error is caught, the component doesn't re-render, and the user never sees it, but I think that's handled with the Yeah, lemme pull over the error handling changes you made for that POC. |
Ported changes from react-redux-hooks-poc Note: the "transient errors" test seems flawed atm.
WebStorm won't recognize tests as runnable if `someFunc.name` is used as the `describe()` argument.
Let's put this on an alpha branch for now. That way if we have to iterate on this any, we can still put out minor builds from master in the meantime. I know any alpha docs do have to go in master, and that's okay, since it's versioned. |
Well, green means go, right? |
A couple quick observations as I work on docs:
|
@markerikson I worked a bit with the hooks on the weekend, and also made an observation. Currently, the selector in Now my comments to your observations:
Indeed, I completely missed this. It would be easy enough to add this parameter, but if we decide to add
I intentionally removed this, since it was only required to ensure the selector never sees inconsistent state and props. Basically, with my implementation it does not matter whether the subscription callback is called after the component was unmounted. The only thing that happens is potentially a
I strongly argue to not make an empty |
const boundAC = useActions(actionCreator : Function, deps : any[])
const boundACsObject = useActions(actionCreators : Object<string, Function>, deps : any[])
const boundACsArray = useActions(actionCreators : Function[], deps : any[]) Unless there is a compelling reason for both, I think it would be good to drop either the 2nd or 3rd version of I probably prefer the object one as it is more auto-documenting within the hook. The hooks that use arrays like |
Personally I think |
I have created a new PR that adds |
I've opened up #1252 for discussion of the alpha itself. Please offer feedback there. |
And just to be clear: WE'VE GOT AN ALPHA! https://github.com/reduxjs/react-redux/releases/tag/v7.1.0-alpha.0 |
… actions (#1248) * add react hooks for accessing redux store state and dispatching redux actions * remove `useReduxContext` from public API * add `useRedux` hook * Preserve stack trace of errors inside store subscription callback Ported changes from react-redux-hooks-poc Note: the "transient errors" test seems flawed atm. * Alter test descriptions to use string names WebStorm won't recognize tests as runnable if `someFunc.name` is used as the `describe()` argument.
… actions (#1248) * add react hooks for accessing redux store state and dispatching redux actions * remove `useReduxContext` from public API * add `useRedux` hook * Preserve stack trace of errors inside store subscription callback Ported changes from react-redux-hooks-poc Note: the "transient errors" test seems flawed atm. * Alter test descriptions to use string names WebStorm won't recognize tests as runnable if `someFunc.name` is used as the `describe()` argument.
… actions (reduxjs#1248) * add react hooks for accessing redux store state and dispatching redux actions * remove `useReduxContext` from public API * add `useRedux` hook * Preserve stack trace of errors inside store subscription callback Ported changes from react-redux-hooks-poc Note: the "transient errors" test seems flawed atm. * Alter test descriptions to use string names WebStorm won't recognize tests as runnable if `someFunc.name` is used as the `describe()` argument.
As discussed in the hooks API design issue, here is an implementation that could be used as an alpha.
There are a couple of open points:
connect.spec.js
are skipped)