-
Notifications
You must be signed in to change notification settings - Fork 128
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
React setState() fails when called from UI-Router Transition Hook #59
Comments
I'm not certain, but it seems to me that the component is not yet mounted. Try registering your callbacks in |
yeah, it sounds like the component is not mounted. Placing the code in |
I tried registering the Which makes sense in retrospect. There was no issue registering the hook. The I've isolated the issue in this project: https://github.com/kboucher/uirouter-test (I'm also trying to setup a plunker, but so far the handler doesn't fire in that context at all.) |
The issue is that the component gets unmounted. See lifecycle in pr: |
@Kukkimonsuta Not sure how that helps I merged your changes and the This works in Angular, but not in React. If this is not a bug, how can I achieve this functionality? |
It's not fix, more of an explanation why it's not working. From my tests I believe ( @elboman correct me if I'm wrong ) a) There seem to be however a bug/feature, where the |
If the handle doesn't fire anymore it sounds like it shouldn't be fired at all, as the parent components gets unmounted during the transition. I'm trying to understand what you want to achieve with this, as I'm not even sure you need a hook here. Transition hooks are useful for modifying a transition while it's happening, or to interact with the rest of the application (outside of the router scope). As @Kukkimonsuta already mentioned, there are to ways to pass data from one state to another, and it depends on your needs and how integrated it needs to be in the app architecture: State paramsEach state param has a type:
Or you can define custom param types. Transition optionsYou can specify custom data in the transition options, and it will be passed along with the transition, using the DifferencesThe difference between the two rely on the use case: state params are more verbose but give you a lot of useful tools. They are explicit and declarative by nature, they are typed, encoded/decoded by default (in the url for example), you can have default values, and so on. (more info here) The The transition prop is not always the same for retained states, if anything changes then the component is re-rendered with the new transition prop. What you are experiencing is the Anyway, I created a simple codesandbox to better illustrate what I mean, let me know if you have any doubts. |
@elboman Thanks for the clarification and posting that example. As you indicated by your statement, I apparently did not need a transition hook to simply pass data from the child to the parent.
I forget now, but I think I went down that path after unsuccessfully trying to the more direct route of accessing the transition
(It was also not necessary to set I'm still a little fuzzy on some of this, but your responses have helped immensely. Thanks! |
I am attempting to pass stateParams from a child route to its parent route:
Child.js
I am attempting to capture this data in the parent route component:
Parent.js
onSuccess
is registered in the Parent component'sconstructor
and fires as expected. I am also successfully retrievingsampleMember
from theparams()
.However,
setState
is consistently aborted by React due to the component's_reactInternalInstance
member beingundefined
.setState
callsenqueueSetState
andenqueueCallback
which both fail (noop) ifthis._reactInternalInstance
is falsey.I am using this same pattern in an Angular version of this sample application (with UI Router) and it works as expected. Am I missing something here, or is this a legitimate bug?
My state definitions (in case it matters):
The text was updated successfully, but these errors were encountered: