-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
create-subscription: call getValue in the constructor is too early #13405
Comments
That's right. However, it checks if the value has changed in
Can you provide a live example demonstrating this? I don't think I fully understand. It's not clear to me how the example you provided corresponds to |
const source = new BehaviorSubject({
status: "initial" | "loading" | "error" | "aborted" | "complete",
})
const controller = {
load: () => { ... },
abort: () => { ... },
}
const Subscription = createSubscription(...)
class Content extends React.Component {
componentDidMount() {
if (["initial", "aborted"].includes(this.props.status)) {
controller.load()
}
}
componentWillUnmount() {
controller.abort()
}
render() { ... }
}
class Container extends React.Component {
render() {
return (
<Subscription source={source}>
{({status}) => <Content status={status} />}
</Subscription>
)
}
} If I remount
|
Is |
@gaearon I tried to make a runnable example yesterday and I found there's no umd version of create-subscription on npm and then I gave it up 😂 I will take a look into CodeSandbox and try to make an online example later. |
https://codesandbox.io/s/wqjq0xml5 @gaearon |
This doesn't look like a bug to me. Eventually What happens is that the child's It seems to me that logic that only checks in |
@gaearon
So checking status in |
Add another status, |
In my opinion, the main problem is, why |
My mental model for So in my view
Because props are the "latest pulled" copy. We'll pull again before the screen gets updated. But there's no guarantee that in any particular lifecycle you're reading the most recent value in props. Rather, the guarantee is that what will end up on the screen reflects the latest value. For the case where the value changes between the initial render and the mount, that second "pull" will happen when the parent wrapper mounts (and will cause a child update). |
There's no difference between these two things IMHO. |
Anyway, thanks for your detailed explanation. |
The difference is that |
Yes, you are right. It's not a problem of create-subscription, it's just what |
👍 Thanks for the repro cases, they've been helpful for the discussion! |
Seems similar with the description 'we can't response to the transition of the data changing' in this comment ? |
Do you want to request a feature or report a bug?
BUG
What is the current behavior?
https://codepen.io/intptr/pen/djEzbr?editors=1010
I made an example to show the execution order of some lifecycle functions while remounting a component:
create-subscription
callsgetValue
inconstructor
and save the result to its state. BeforecomponentDidMount
called, any changes will be ignored.If I remount a component wrapped by
create-subscription
component, and do something in itscomponentWillMount
which will modify the source value, I will get the wrong value incomponentDidMount
in the new component.What is the expected behavior?
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.4.2 (latest)
The text was updated successfully, but these errors were encountered: