-
-
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
Rule idea: no spreading props #1094
Comments
In other words, forbidding spread props, but just of the entire props object? This seems pretty useful. I'd actually want to forbid spread props in all cases, so that maybe could be configurable. |
Yeah, a configuration would be good, I think. Sometimes we like to assign local props or use a function to pull them out, which I think we'd like to still be able to do. function MyComponent(props) {
return <MySubComponent {...subComponentProps(props)} />
}
function subComponentProps(props) {
return {
one_prop,
two_prop: two_prop * 2
};
} Haven't landed on a clear style for that, though. |
If no one else is actively working on this I wouldn't mind taking a shot. I've run into a few cases where as components have grown using |
@thoiberg you can use https://npmjs.com/prop-types-exact on each of your components to help catch that, but the linter rule is also great :-) |
Just adding that I'd like to see this rule added as well since there are potential performance implications, as noted here: https://flexport.engineering/optimizing-react-rendering-part-1-9634469dca02#63e7. |
@thoiberg did you ever get anywhere on this? |
@benmonro no sorry, I got a little way's in but work got in the way 😞. Feel free to take over if you have the desire 😄 |
I think that rule was about to not use this syntax:
and use that instead:
Reasons:
|
->
|
@iegik yeah, that could work if it's too hard to implement my suggestion above. |
yes this is needed. |
I can take this up, will raise a PR soon |
This is more or less solved by Typescript. In my current project, I no longer have this problem, as I am passing and combining types for props. In fact, in a type-safe world, it actually makes more sense to spread props to allow the full range of native props with auto complete. type LoaderButtonProps = ButtonHTMLAttributes<HTMLButtonElement>
& { customProps... };
export default function LoaderButton(props: LoaderButtonProps) {
const [ isLoading, setLoading ] = useState(false);
//(this could be optimised, I know)
const handleClick = props.onClick && (e: MouseEvent<...>) => {
setLoading(true);
props.onClick(e).then(() => setLoading(false));
}
if(isLoading) return <Loader/>;
return <button {...props} onClick={handleClick} />
} |
This presumes that every component is defined to only allow exact props (which they’re not by default in TS) and that TS types are capable of fully expressing the type for all JS values (it isn’t), and that all components you use are authored in TS (unlikely if you use anything from npm). |
I agree. in TS files, I found this pattern to be useful enough: import Rainbows ...;
import { LoaderButton, LoaderButtonProps } from '../LoaderButton';
type SuperProps = {
rainbow: boolean;
}
type SuperButtonProps = LoaderButtonProps & SuperProps;
function SuperButton(props: SuperButtonProps) {
//in case of conflict, composing component should decide what props go where
// e.g. in case of className
const { rainbow, className, children, ...baseProps } = props;
return <LoaderButton {...baseProps} >
{rainbow && <Rainbows className={className}/>}
{children}
</LoaderButton>;
} what do you think? |
I think you should explicitly pass the props you need, only. |
One thing we've noticed tends to make code a little opaque is when we destructure the entire props for a sub-component:
It would be great to have an eslint rule to force expanding individual props that should be passed down:
The text was updated successfully, but these errors were encountered: