-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
no-unused-prop-types false positive when prop is used in function #933
Comments
It seems like a very strange pattern to pass There's a limit to how much a linter can follow values being passed around, and passing around |
Yes, but that's how the codebase on the project I've just recently started working on looks. I'm setting up linting rules for it and I ran into this problem and thought I should at least report it. I get that following However I have no idea how the linting works under the hood as I haven't looked at the code, yet, and it might not be possible at all. Anyways, I'll probably just refactor things a bit as it's an unusual pattern. |
Just a a side note, I believe the pattern was used to workaround binding of class methods. |
Binding of class methods in the constructor is the proper and idiomatic way to handle these things in React, fwiw. Using arrow functions as class properties is actually much less efficient, because it creates the entire function N times - whereas a constructor-bound prototype method is created once, and the only thing created N times is a tiny wrapper function (the .bind) that calls into the shared, optimizable method. |
Would that hold true for using Thank you for the insight, I didn't realize the impact of arrow function as class properties. |
Yes, the bind operator is identical to using The best is to do |
Anyways, if you really feel that this case shouldn't be supported then feel free to close this. At least the conversation is on record and anyone else running into this will know that there's a better way to solve this. |
The issue should stay open in case there is a way to implement it :-) sorry for going offtopic with the code style discussion. |
Theoretically, I see your logic, but do you have any numbers on this? I can't see this possibly making a noticeable difference unless you're rendering thousands of instances of the same component. Anyway, back to the OP, I'm having this issue without the use of
// @flow
import React from 'react';
type Props = {
onMouseOver: Function, // 'onMouseOver' PropType is defined but prop is never used
onClick: Function,
};
const MyComponent = (props: Props) => (
<div>
<button onMouseOver={() => props.onMouseOver()} />
<button onClick={() => props.onClick()} />
</div>
);
export default MyComponent; |
@robwise "numbers" would all be either app-specific, or microoptimizations that aren't worth calculating :-) It may indeed not make a noticeable difference - but I find it both theoretically more performant and actually more correct and maintainable. ¯_(ツ)_/¯ I'm assuming that if you convert the Flow back to propTypes, that everything's fine - if so, then this is a bug with the plugin's Flow parsing. Could you also try using |
See, I feel the opposite about maintainability, because you have to remember to add the binding in the constructor for a method that lives somewhere else in the file, and then if you delete said method, you have to remember to go find its binding and remove it again. With fat arrow class properties, when you delete your method, you're done.
If I convert to use PropTypes instead of Flow, same problem: const MyComponent = (props) => (
<div>
<button onClick={() => props.onClick()} />
<button onMouseOver={() => props.onMouseOver()} />
</div>
);
MyComponent.propTypes = {
onMouseOver: React.PropTypes.func.isRequired,
onClick: React.PropTypes.func.isRequired, // 'onClick' PropType is defined but prop is never used
};
export default MyComponent;
There's no error if I destructure: const MyComponent = ({ onMouseOver, onClick }) => (
<div>
<button onClick={() => onClick()} />
<button onMouseOver={() => onMouseOver()} />
</div>
);
MyComponent.propTypes = {
onMouseOver: React.PropTypes.func.isRequired,
onClick: React.PropTypes.func.isRequired,
};
export default MyComponent; |
Here's a simplified example.
We're using flow, but I guess this will fail when using
propTypes
also.The text was updated successfully, but these errors were encountered: