-
Notifications
You must be signed in to change notification settings - Fork 142
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
Easy way to register to for a one time promise callback? #58
Comments
@ryanbrainard, can you explain how it helps in the scenario I described? I actually want to put the promise value into the wrapped component state (for example). |
FWIW, I also have had to do this, and have been using a pattern like this:
But it would be nice if the |
@arbesfeld this works because onDismiss() will close the view component you are in. Really surprised such a classic scenario does not have an answer in RR. I really like the design pattern of RR, its really simple and declarative, no boilerplate code. I wish the HOC will call a callback I defined, |
@ryanbrainard any idea? |
What I ended up doing was wrapping my component with my own HOC below the Connect HOC, which middlewares the willReceiveProps lifecycle method and checks for a change in promiseState.settled value between this.props and nextProps, and triggers an exposed function on the wrapped element. I wish it was a built in function though |
@eyalw Sorry for the delay getting back to you. I would like to support your use case, but I'd like to understand it a little better: Is there a reason you want to pass in the callback as a ref? Does that give any advantage over proving a function that takes the wrapped component as an argument? I don't use refs very much, so perhaps I'm missing something.
I don't think that What if connect({
fetchAccountPs: {
method: "GET",
url: "/some/url",
andThen: (_, meta) => {
meta.wrappedComponent.onAccountFetched()
return {};
}
}
)... Not sure if this is the best place to expose this, but just spitballing a bit. I'd like to do this in a supported, clean way, but would like to understand the requirements a bit better to see if RR should be calling the function on your behalf, if the promise should be exposed in |
@ryanbrainard I like your idea. Your API seems more flexible, and you can control the function being called better, and add conditions and pass different values. I do also like it when an API offers me to code the common case in shorter way, which I think is: As to your question about my use case, its mostly stuff like: onFetchAccountResponse(err, account) {
if (!err)
this.setState({ account }); // load data into a state-binded-form-fields
}
onSaveAccountResponse(err) {
if (!err) {
showNotification('success', 'Wohoo! Account saved'); // one time GUI notification
this.context.router.redirect('/somwhere'); // redirect
}
}
onChangePasswordResponse(err) {
if (!err) {
showNotification('success', 'Your password was changed'); // notify again, and:
this.setState({ formData: null }); // clear the form fields
}
} |
I'll chime in in support an implementation for this behavior, as #66 (duplicate of this issue, effectively) illustrates my use case. Also, @hnordt's solution doesn't work for me since I'm using You can still workaround that fact using the singleton history, but you're going to run into issues with universal apps with differing history strategies (in-memory vs browser). |
@ryanbrainard I'm interested to learn, when andThen() will be called, and will make a call to a function on my component, what is the props of the component at that instant? are they already updated with the new fetched data? was willReceiveProps already called for the wrapped component or not? |
@eyalw
The prop should be
yes
yes I think what you are getting at here is that it might not work since it's already updated. connect({
fetchAccountPs: {
method: "GET",
url: "/some/url",
then: (value, meta) => {
meta.wrappedComponent.onAccountFetched()
return {value, meta};
}
}
)... So, if we create a new place to do this kind of thing, where would be the best place in here so that to happen? ...or, would |
having both then and andThen as 2 different times to hook into - before and after the props have passed down to the wrapped component - seems great to me. |
I would really be happy to see @ryanbrainard suggestion implemented. |
+1 |
2 similar comments
+1 |
+1 |
I encountered the same scenario as @eyalw. Where I just want to trigger an action eg. |
@ryanbrainard any updates on this issue? I think meta.wrappedComponent is a very good addition to the API. |
In my case, I want to show a dialog window when the result was fulfilled. While the fetch promise prop doesn't exist, the dialog is not shown. A one time callback like proposed here will let me set the dialog state. Something like this could work (not tested): connect(props => ({
shouldShowDialog: { value: false },
dismissDialog: () => { value: false},
postStuff: data => ({
stuffRequest: {
method: 'POST',
url: '/stuff',
andThen: () => ({
shouldShowDialog: { value: true }
})
})
}))(MyApp) Then my code could use the value in That said, is there anything limiting us from passing connect(props => ({
postStuff: data, component => ({
stuffRequest: {
method: 'POST',
url: '/stuff',
andThen: () => ({
component.doStuff();
return {};
})
})
}))(MyApp) |
#58 Added wrappedInstance to the meta object
Makes sense. What about the proposal of dismissing a fetch, back to how it was before it was fetched? So we could have rendering like this example: |
Can we close this issue now? |
Rendering different promise statuses (pending, setteled, etc) is easy (yay!).
Performing a function one-time once a promise settles seems hard.
It's very common that I want to perform some logic when a promise is settled - for example:
if i do this:
It gets called every time props change, even though the fetch promise does not change, not good. I only want it to get called once. when (ps.pending => ps.setteled).
if the promise mapping changes and fetch occurs again, only then call the .then() again.
right now I need to keep track of when (ps.pending => ps.setteled) happens myself which is tedious.
would be really nice to be able to do
and the HOC would call my component exposed onAccountFetched(value) once, using a ref.
what do you think?
is there another existing way i'm missing?
The text was updated successfully, but these errors were encountered: