Skip to content

Instantly share code, notes, and snippets.

@emilong
Created October 13, 2016 20:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emilong/263aaed767b8e0e5ee6496948e138d47 to your computer and use it in GitHub Desktop.
Save emilong/263aaed767b8e0e5ee6496948e138d47 to your computer and use it in GitHub Desktop.
function WhySoManyProps(props) {
const user = extractUser(props);
const fudge = calculateFudge();
const bits = computeBits();
// This is soooooo redundant.
return <SomeComponent user={user} fudge={fudge} bits={bits} />;
}
function Shorthand(props) {
const user = extractUser(props);
const fudge = calculateFudge();
const bits = computeBits();
// spread all the new props which have the exact same name as the props
// for the child component using ES6 syntax.
return <SomeComponent {...{ user, fudge, bits }} />;
}
@tonilaukka
Copy link

We can make it even more cleaner.

function Shorthand(props) {
  const someProps = {
    user = extractUser(props),
    fudge = calculateFudge(),
    bits = computeBits()
  };

  return <SomeComponent {...someProps} />;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment