Skip to content

Instantly share code, notes, and snippets.

@yuvalbl
Last active June 22, 2021 19:18
Show Gist options
  • Save yuvalbl/49111ced0ff6f75975472ad2fbddd7f3 to your computer and use it in GitHub Desktop.
Save yuvalbl/49111ced0ff6f75975472ad2fbddd7f3 to your computer and use it in GitHub Desktop.
react_spread_attributes
function App1() {
return <Greeting firstName="Ben" lastName="Hector" />;
}
function App2() {
const props = { firstName: 'Ben', lastName: 'Hector' };
return <Greeting {...props} />;
}
import { Button } from '@material-ui/core';
export const PrimayButton = (props) => (
<Button color="primary" {...props} />
);
export const Button = ({ color, variant, disableElevation, size, children }) => {
// calculate style according to props and render
};
<PrimayButton>My Button</PrimayButton>
<PrimayButton variant="contained" disableElevation size="large">My Button</PrimayButton>
import { Button } from '@material-ui/core';
export const PrimayButton = ({ size }) => (
<Button color="primary" size={size} />
);
<PrimayButton2 size="large">Large Button</PrimayButton>
<PrimayButton2 size="medium">Medium Button</PrimayButton>
<PrimayButton2 size="small">Small Button</PrimayButton>
const useFormInput = (initialValue) => {
const [value, setValue] = useState(initialValue);
const handleChange = (e) => {
setValue(e.target.value);
};
return {
value,
onChange: handleChange
};
};
// usage
const App = () => {
const name = useFormInput('Mary');
return (
<div>
<input {...name} />
<button className="action-button">x</button>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment