-
Notifications
You must be signed in to change notification settings - Fork 4
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
Ref as an observable #5
Comments
Hmm, I feel like there was a reason but I can't think of it now. Perhaps we can subscribe in the constructor... Also can you explain to me why BehaviorSubject worked but funcSubject did not? Is it something we can fix? |
Oh yeah, it's to prevent calls to |
It's because I'm not sure how it could be fixed. Changing Maybe preventing calls to setState could be done on the subscribe side? Something like wrapping |
Ah I see! Couldn't we just provide two different versions of |
Yeah, that would work. Generally those two should cover most things; I really do think it's better to subscribe once and batch changes between construct and componentDidMount though. I'm having a hard time explaining why... |
@heyimalex There is only one subscription. Or are you referring to this part? https://github.com/acdlite/react-rx-component/blob/master/src/createRxComponent.js#L5-L9 I wish we could avoid that but it's the only way to set the initial state in the constructor. Do you have a suggestion for how this could be better? |
Yeah, I was referring to that. I think I'm afraid of hot/cold observable confusion and subscribe time side effects, but I really can't think of a good rational example right now so it's probably fine. I have a branch that avoids it in a different way but I'm still working on it. |
Thanks for investigating! It doesn't look like your branch sets the initial state in the constructor, though. We need that for the initial render. |
It depends I think? Rx observables will execute synchronously if they can, so if childProps$ is synchronous the subscription will fire before I change |
Ah, but I don't think you're allowed to use |
Haha well the really roundabout way is just to do: let onNext = childProps => this.state = childProps;
// then
onNext = childProps => this.batched = childProps;
// then
if (this.batched) { this.setState(this.batched); }
onNext = ::this.setState; |
Actually that's a great point — we can just update the observer to check if the component is mounted and do either |
The extra subscription has been removed. I'll leave this open until we update funcSubject to allow the option to be "cold." Or maybe we need a better name for funcSubject? Can't think of any, though. |
This actually should resolve the issue with refs; as we're subscribed when the ref callback fires, the value isn't lost and so there's no need to use a BehaviorSubject. That being said, adding a But I actually re-reviewed that code in #6 and I think I see a problem! The ref callback will fire between I checked up on the setState-in-constructor thing and it looks like you're right. Do you know if you can use |
@heyimalex I'm not sure if componentWillMount works, will have to check. We'll need to find another solution for refs, by the way, because ref callbacks are being removed in 0.14: https://facebook.github.io/react/blog/#dom-node-refs |
I didn't read that as they are removing ref callbacks, just changing what refs on React.DOM elements resolve to. The last time I looked they considered string refs "legacy" and callback refs the way forward. |
Ah I hope you're right. That would be nice. |
This came up today trying to pass a
funcSubject
as a ref callback function. Because we're not actually subscribed when the ref callback fires (ie beforecomponentDidMount
), the value is just kind of lost.Using a BehaviorSubject instead fixed the issue, but I thought I'd mention it for anyone else who ran into it.
Also I was wondering why you subscribe in componentDidMount and not the constructor? It seems like it potentially adds some unnecessary work. I imagine it's a "universal" thing but I don't have any experience with that...
The text was updated successfully, but these errors were encountered: