How do you make a Media component in React?
```jsx
var Media = React.createClass({
});
```
We then need to export our component so it can be used outside this file.
```js
module.exports = {
Media: Media
};
```
Every component needs a render function
This tells the browser what HTML to render.
// just rendering a paragraph for now
// `this.props.children` tells react where to put any child nodes.
```jsx
var Media = React.createClass({
render: function () {
return (
{this.props.children}
);
}
});
```
Let's try it. Sweet, this is gonna be rad!
```html
My very first React component!
```
When React renders that component we get this html
```html
My very first React component!
```
Not very useful, right? Well, you'd never use react to render a simple paragraph because it already understands `` tags, you can just use them!
What if we want to start building an actual media block?
Let's add a left image.
# What did we try first?
# Tons of attributes
```html
Media block content
```
How does it work? Well, remember the render function in our component?
It snags the leftImageSource attribute and sets it as our image source.
(for now we'll focus on api stuff)
So, we thought we were pretty clever, then we realized sometimes the image was a link, so we added an `href`.
```html
Media block content
```
```html
Media block content
```
Oh, and we need to handle the images vertical alignment
```html
Media block content
```
Oh shoot, accessibility, we need an alt atribute.
```html
Media block content
```
Oh, and performance! We need a height and width!
```html
Media block content
```
The space between the image and the content can vary, so we'll need `leftImageSpacing`.
```html
Media block content
```
Oh, and we'd better add all the properties for the media block itself
Including stacksize (breakpoint) and bodyAlignment (vertical alignment of body content)
```html
Media block content
```
This is getting out of hand!
But we forgot something, the media block can also have a right image, so we'll need to duplicate all that.
```html
Media block content
```
This is out of control, we are going the **wrong way**!
What is working here?
* It's very explicit, we know what each thing does
What isn't?
* We're basically recreating html in React, yuck! (we shouldn't make a new different alt attribute!)
* We have image properties and media properties all mixed up
*
So what did we think of next?
# JSON
```js
var images = [
{
"src": "http://placehold.it/50x50",
"href": "http://www.google.com",
"alignment": "middle",
"alt": "profile photo",
"height": "50px",
"width": "50px"
},
{
"src": "http://placehold.it/50x50",
"href": "http://www.google.com",
"alignment": "middle",
"alt": "profile photo",
"height": "50px",
"width": "50px"
}
];
```
Which we can put into our Media component like so:
```html
Media block content
```
We never actually built this, because we weren't satisfied with it, but let's talk about what works and doesn't
What works?
* cleaner separation of concerns (media takes care of media stuff, rather than the properties of it's children)
What doesn't work?
* It isn't that different than what we had before
* The abstraction of passing in JSON means all the code isn't in the same place
* It's a little weird to have JSON in the middle of what looks like markup (IMO)
* We're really still reinventing html atributes that react would give us for free on an `
` element.
So, what did we try next?
# Parsing Children
We decided to try including the images as children.
```html
My media content
```
This looked better, everything is normal html! But, it has a few drawbacks.
What works?
* Normal HTML
* Facebook does it this way
What doesn't work?
* The images and body content need to be in a very particular order, it seems weird to expose that to the user
* Violates the *"build components you can use without understanding CSS"*
* We could loop over children and reorder them, but how do we tell the difference between content images and media images?
* We were still discovering React, and didn't know *how* to loop over children yet
* React provides handy error messages and property validations. We would lose out on that if we made the images children
* Facebook's images aren't optional, so it's a different case
So what did we try next?
# React built in `
` Component
In react, people joke that everything is a component.
It's not a joke, everything is.
First, we make our image.
```jsx
var leftImage =
;
var rightImage =
```
Next, we make our media object.
```html
Media block content
```
You'll notice this looks similar to the JSON example.
You can even write it like this if your really want to.
```html
}
bodyAlignment='middle'
stackSize='medium'>
Media block content
```
What works?
* React passess default html attributes in to the resulting img tag, so we don't have to do anything special with height, width, src, and alt.
* Aria values would also be passed through
* We separate concerns and the image takes care of it's own properties
* No need to parse the content
What doesn't work?
* HTML inside an attribute (in the latter example) is a bit odd, though it does have advantages.
* Remember how I said default HTML attributes are passed through? The href will also be passed through. So our image will have an href attribute. I like clean html, and that feels weird to me!
```html