Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Components: Playing simple and easy

React Components: Playing simple and easy

Light talk on some of the good practices for React Components.

Presented at Moo Tech Tuesday.

http://mootechtuesday.eventbrite.co.uk

Henrique Alves

June 01, 2016
Tweet

More Decks by Henrique Alves

Other Decks in Programming

Transcript

  1. Low level components 
 They represent the HTML element in

    your document. @healves82 #TechTuesday // layout/image.js // e.g. <Image src="cover.png"/> const Image = ({...props }) => ( <img {...props} /> );
  2. Low level components @healves82 #TechTuesday /* const CoverImage = ({title,

    src}) => ( <img src={src} alt=`Front Cover – ${title}` /> ); */ const CoverImage = ({title, src}) => ( <Image src={src} alt=`Book Cover – ${title}` /> );
  3. Low level components @healves82 #TechTuesday // layout/image.js const Image =

    ({...props }) => ( <img {...props} /> ); // components/cover-image.js const CoverImage = ({title, src}) => ( <Image src={src} alt=`Book Cover – ${title}` /> );
  4. Low level components @healves82 #TechTuesday // image.js const Image =

    ({...props }) => { const style = { borderStyle: 'none' }; const styles = Object.assign({}, style, props.style ); return <img {...props} style={styles} /> };
  5. You know what the component does by looking at its

    propTypes. @healves82 #TechTuesday
  6. PropTypes @healves82 #TechTuesday BookItem.propTypes = { coverImage: React.PropTypes.element.isRequired, title: React.PropTypes.string.isRequired,

    author: React.PropTypes.string.isRequired, description: React.PropTypes.string, price: React.PropTypes.number.isRequired };
  7. Type checking with Flow @healves82 #TechTuesday type Props = {

    coverImage: CoverImage, title: string, author: string, price: number }; const BookItem = ({coverImage, title, author, price, ...props}: Props) => ( ... );
  8. Any styling logic is added into the component not in

    the CSS. @healves82 #TechTuesday
  9. Styling @healves82 #TechTuesday const Button = () => ( <div

    className={styles.root}> <Text weight="bold">Button</Text> </div> );
  10. Atomic CSS Modules Michele Bertoli suggested an interesting approach combining

    Atomic CSS with CSS Modules. @healves82 #TechTuesday .tabs { composes: width-100 from 'size'; composes: reset from 'list'; composes: flex from 'display'; /* tablet and up */ composes: block from 'display/tablet'; }
  11. Helpers for inline styles @healves82 #TechTuesday // gradient.js // You

    can find some useful helpers at 
 // https://github.com/kitze/stylz export const linear = (fromColor, toColor) => ({ background: `linear-gradient(to bottom, ${fromColor}, ${toColor})` }); // button.js import {linear} from '../helpers/gradient.js'; const styles = { padding: 8, linear('#eee','#ddd') };
  12. Don’t be religious. Don’t knock it without try. Pick the

    one that works better for your team. @healves82 #TechTuesday