-
Notifications
You must be signed in to change notification settings - Fork 2
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
Ignored static properties of SFCs (Stateless Functional Components) #3
Comments
P.S. I suppose |
Very good points. Calling a SFC directly (as I'm doing) indeed does bypass PropTypes validation, which it shouldn't. I've been pondering this and have come up with the following. If the code is running in I know that this doesn't answer your Thoughts? p.s. I'm also thinking that you could control this behavior with a static 'Component.options` object, but I'm not as set with this just yet. |
I think your proposed solution is logical, I just wonder if it might ever cause issues that the production behavior would be different than in development...perhaps there are some edge cases where code could depend on the component being mounted instead of just called as a function. This seems unlikely but I would be a little concerned with this mainly because it would happen behind the scenes, and probably not be apparent to the programmer. If you still want to provide the performance optimization in the case of SFCs with
Here's one of the places where they call it: The only issue I can think of with calling it directly rather than letting React handle it would be the stack traces. But I'm guessing that's a solvable problem, and in any case the Your proposed solution might be fine, but I think it's worth considering this alternative. Thanks! |
I implemented my solution for now (published on npm as v1.1.0), but could always be open to circling back if there is a stong case to do so. I'll go ahead and close this for now. Thx for your thorough consideration of the problem and for raising the issue. fixed with #4 Also: see https://codesandbox.io/s/6j2k7z35nn too see prop types warning |
Thanks! And as to |
You can open a separate issue for just |
Oh, I just noticed that the way you implemented it (the handling of |
@mbrowne I need to clarify your clarification... My approach MAY result in different behavior in development vs. production IF you use a plugin for dead code/propType elimination based on If static I see this as a good thing, but if you have problems because of this, file an issue with your specific use case. |
Ah ok, I understand it better now, thank you. |
This library respects the use of
defaultProps
on SFCs, which is good (although in practice you can often just use default argument values for SFCs), but there are other properties an SFC can have that are currently ignored:I realize that calling a function directly is more efficient than mounting it as a component [1], so skipping
displayName
makes sense, but IMOpropTypes
andcontextTypes
should still be respected, if they exist. So in those cases I would think it would be better to go ahead and render the SFC as a component rather than calling it directly.Example of PropTypes being ignored:
[1] FWIW, SFCs have new optimizations applied to them in React 16 that make them faster than regular class components ("regular class" meaning
React.Component
without ashouldComponentUpdate
method...as to SFC vs.React.PureComponent
, it depends on the situation).The text was updated successfully, but these errors were encountered: