React basics

First impressions

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './index.css';

class App extends Component {
    render() {
        return (
            <div className="App">
                <h1>Hello, React!</h1>
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById('root'));
import React from 'react';
import ReactDOM from 'react-dom';

class AddFriend extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      newFriend: ''
    }

    this.updateNewFriend = this.updateNewFriend.bind(this)
    this.handleAddNew = this.handleAddNew.bind(this)
  }
  updateNewFriend(e) {
    this.setState({
      newFriend: e.target.value
    })
  }
  handleAddNew() {
    this.props.addNew(this.state.newFriend)
    this.setState({
      newFriend: ''
    })
  }
  render() {
    return (
      <div>
        <input
          type="text"
          value={this.state.newFriend}
          onChange={this.updateNewFriend}
        />
        <button onClick={this.handleAddNew}> Add Friend </button>
      </div>
    )
  }
}

class ShowList extends React.Component {
  render() {
    return (
      <div>
        <h3> Friends </h3>
        <ul>
          {this.props.names.map((friend) => {
            return <li> {friend} </li>
          })}
        </ul>
      </div>
    )
  }
}
class FriendsContainer extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      name: 'Tyler McGinnis',
      friends: [
        'Jake Lingwall',
        'Sarah Drasner',
        'Merrick Christensen'
      ],
    }

    this.addFriend = this.addFriend.bind(this)
  }
  addFriend(friend) {
    this.setState((state) => ({
      friends: state.friends.concat([friend])
    }))
  }
  render() {
    return (
      <div>
        <h3> Name: {this.state.name} </h3>
        <AddFriend addNew={this.addFriend} />
        <ShowList names={this.state.friends} />
      </div>
    )
  }
}

ReactDOM.render(<FriendsContainer />, document.getElementById('root'));

Step by step

Components

FilterableProductTable

SearchBar

ProductTable

ProductCategoryRow

ProductRow

ProductTable Component

function App() {
  return (
    <div className="App">
      <h1>Hello World</h1>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

ProductTable Component

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const PRODUCTS = [
  {
    category: "Sporting Goods",
    price: "$49.99",
    stocked: true,
    name: "Football"
  }
];

const ProductTable = () => {
  return (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        {PRODUCTS.map(product => {
          return (
            <tr>
              <td>{product.name}</td>
              <td>{product.price}</td>
            </tr>
          );
        })}
      </tbody>
    </table>
  );
};
function App() {
  return (
    <div className="App">
      <h1>Hello World</h1>
      <ProductTable />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

props

ProductRow Component

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const PRODUCTS = [
  {
    category: "Sporting Goods",
    price: "$49.99",
    stocked: true,
    name: "Football"
  }
];

const ProductTable = () => {
  return (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        {PRODUCTS.map(product => {
          return <ProductRow product={product} />;
        })}
      </tbody>
    </table>
  );
};
const ProductRow = ({ product }) => {
  return (
    <tr>
      <td>{product.name}</td>
      <td>{product.price}</td>
    </tr>
  );
};

function App() {
  return (
    <div className="App">
      <h1>Hello World</h1>
      <ProductTable />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

state

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const PRODUCTS = [
  {
    category: "Sporting Goods",
    price: "$49.99",
    stocked: true,
    name: "Football"
  }
];

const ProductTable = ({ searchValue }) => {
  let products = PRODUCTS.filter(product => product.name.includes(searchValue));
  return (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        {products.map(product => {
          return <ProductRow product={product} />;
        })}
      </tbody>
    </table>
  );
};

const ProductRow = ({ product }) => {
  return (
    <tr>
      <td>{product.name}</td>
      <td>{product.price}</td>
    </tr>
  );
};
const SearchBar = ({ searchValue, handleChange }) => {
  return (
    <form>
      <input
        type="text"
        placeholder="Search"
        value={searchValue}
        onChange={handleChange}
      />
    </form>
  );
};


class App extends Component {
  state = {
    searchValue: ""
  };

  handleChange = e => {
    this.setState({ searchValue: e.target.value });
  };

  render() {
    return (
      <div className="App">
        <h1>Hello World</h1>
        <SearchBar
          searchValue={this.state.searchValue}
          handleChange={this.handleChange}
        />
        <ProductTable searchValue={this.state.searchValue} />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Why?

What's next?

  • Life Cycle
  • High Order Component
  • Hooks

React basics

By Kattya Cuevas Montes

React basics

  • 135