-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Add invariant to check getInitialState return value. Fixes #397 #725
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
Conversation
|
@cpojer if you want to create an upstream diff I'll stamp it |
|
You say it can be null but no tests to confirm (also doesn't look like that's true) |
|
@zpao |
|
Added a test that ensures getInitialState can return null. |
|
👍 |
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.
undefined should also be valid. Otherwise a function like this would fatal:
getInitialState: function() {
if (something) {
return {some: 'abc'};
}
}
|
@vjeux Not saying it's right, but there's a merit to being explicit as well, if you define a function, you return a value (be it |
|
Personally I'd like to be explicit. If you define a function called 'getInitialState' I think it must always return something and 'undefined' is a bad initial state. However, if this breaks a lot of code we should consider allowing it. |
|
Ok I'm sold, let's do it :) |
|
TLDR; I agree. A component that has null or undefined should be treated as a stateless component. Debugging tools may use it to determine whether this is a stateful component or a simple one. We might be able to do optimizations by knowing that a component isn't stateful. I could see the benefit of being able to determine if a component should be stateful at runtime. That should probably not be determined by props since that always makes it potentially stateful. Class level flags you can probably just make the whole getInitialState function null if you need to. |
|
@sebmarkbage Part of the discussion was about whether |
|
Right, but perhaps we should also forbid null while we're at it? |
|
@sebmarkbage I can't come up with any real scenario that would need it (and obviously none would require it). Theoretically you may want to only have a state sometimes, but again, it wouldn't require it and if you absolutely must optimize that specific case, just swap the entire function for null based on the condition. So yeah, I'm inclined to agree with you. |
|
If you don't need any state, you can |
|
Isn't |
|
I don't really care tbh |
|
@sebmarkbage what do you say? |
|
Some people use dynamic property keys to add to the object later. Returning an object demonstrates the intension that the component is stateful. An empty object may not have all it's keys yet but will at some point. I guess we wouldn't want to overload that intension. We should probably just allow null until we can figure out when this is used, if at all. Don't really care. I'm just thinking out loud. You should get this is in. |
|
Upstream diff is accepted and ready to land, so I'm merging this. |
Add invariant to check getInitialState return value. Fixes #397.
This adds an invariant that checks if getInitialState returns an object. Let me know if I should change the error message to something different.