Diving into React: The Final Module of Flatiron School
Tags: beginners, react
Published: 2019-12-29 21:21:09

I'm in my fifth month of Flatiron's coding boot camp, and the time has simultaneously flown by while feeling like years. In the fifth and final module of Flatiron, I am learning React and Redux. Since React has felt very different from anything we've done so far, I figured now would be the perfect time to blog about some beginner concepts--components and props.

Programmers love the separation of concerns, and - based on my current understanding - React does an excellent job of making separation very easy.

<h2>Components</h2>

React has a class called Component which is used to declare and components built into your application. Any components made are technically classes that inherit from the Component class. Components are used to separate aspects of our application that will need to be appended to the DOM.

```react
class Welcome extends React.Component {
  render() {
    return (
      <div>
        Hello World!
      </div>
    )
  }
}
```

To render that component to the DOM, the component is added in the App class using JSX (a tag language that is basically a combination of JS and XML to render HTML):

```react
class App extends React.Component {
  render() {
    return (
      <div>
        <Welcome />
      </div>
    )
  }
}
```

This would append the 'Hello World' div to the ReactDOM in our application. Components become more dynamic by adding properties or 'props'.

<h2>Props</h2>

Let's say we wanted the welcome message in our application to be customized by name. We can do this by adding a 'name' prop to our Welcome component.

```react
class Welcome extends React.Component {
  render() {
    return (
      <div>
        Hello {this.props.name}!
      </div>
    )
  }
}
```

Then when we call our component in our App class we can pass in a name prop to our welcome message.

```react
class App extends React.Component {
  render() {
    return (
      <div>
        <Welcome name="Laura" />
        <Welcome name="John" />
        <Welcome name="Audrey" />
        <Welcome name="Charlie" />
      </div>
    )
  }
}
```

This would append 4 customized welcome messages to our ReactDOM. As the code stands now, the App class is still hardcoded, but this can be easily altered by iterating over an object containing the name data.

<h3>Default Props</h3>

If we also wanted to pass in a default name in case none was given, React has a 'defaultProps' method that makes this easy and very readable. After declaring our Welcome component, we can declare the default props we want to include.

```react
class Welcome extends React.Component {
  render() {
    ...
  }
}

Welcome.defaultProps = {
  name: "stranger"
}
```

Et voilà, that is my simple intro to React components and props. So far I have been enjoying learning React. I love when code is very readable and clean, and that seems to be deeply ingrained in React. The concepts seem pretty logical and self-explanatory - but of course, I am just beginning to scratch the surface. I'm excited to dig deeper (next I'm learning about forms and events) and begin building some React apps.

React docs: https://reactjs.org/docs/getting-started.html

<h4>Happy new year to all!</h4>

My goal in 2020 is to continue building and learning. I'd like to dive into Swift (and maybe Kotlin) once I feel my understanding is deep enough in Ruby on Rails, JS/HTML/CSS, React, and Redux.