Custom Component

Loading "Custom Component"
๐Ÿ‘จโ€๐Ÿ’ผ We want to reduce duplication here by making a custom <Box /> component that renders a div, accepts all the props and merges the given style and className props with the shared values.
I should be able to use it like so:
<Box className="box--small" style={{ backgroundColor: 'lightblue' }}>
	small lightblue box
</Box>
The box className and fontStyle: 'italic' style should be applied automatically by the <Box /> component which should then merge that default with the values that come from props.
How you go about implementing it is part of the fun (aka "learning" ๐Ÿ˜‰). You may come up with something completely different from how I do it and that's great for your learning. When you're done, these two ultimately render exactly the same thing:
// before:
<div
  className="box box--small"
  style={{fontStyle: 'italic', backgroundColor: 'lightblue'}}
>
  small lightblue box
</div>

// after:
<Box className="box--small" style={{backgroundColor: 'lightblue'}}>
  small lightblue box
</Box>
And I should be able to switch the div for a Box for the medium and large boxes as well.
๐Ÿ’ฐ Here's a quick tip for the TypeScript part of this. When you want to wrap an element to basically simulate that element + a little functionality (like in our case), you'll want to borrow the type definition for that element from React. Here's how you do that (for a span element rather than div):
function MySpan(props: React.ComponentProps<'span'>) {
	return <span {...props} />
}
And now you'll get type safety and autocomplete for all the HTML attributes accepted by a span element.
Login to get access to the exclusive discord channel.
  • โš›๏ธFundamentals
    First Four Exercises are Free!
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    To make sure this shows up in the fundamentals thread. Isn't it exciting the first four exercises of...
    1 ยท 4 days ago
  • General
    Welcome to EpicReact.dev! Say Hello ๐Ÿ‘‹
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    Welcome to the first of many posts in the EpicReact.dev channel! Take a moment to introduce yourself...
    0 ยท 4 days ago