davidmarsland / react-labs

React Dev Labs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#react-labs

React Dev Labs

These labs augment the React and Redux course at

https://davidmarsland.github.io/react-spike/

Lab solutions are available here:

https://github.com/davidmarsland/react-labs/


Lab: Set Up React Dev Env and Create React App helloworld


Tutorial Labs: Intro to React

Facebook React Tutorial


Lab: Simple Table in React

Implement a simple table in React

Here's the app you're about to create: Lab Solution Online

  • Generate project
create-react-app simpletable
  • In src directory, delete App.*
  • Start with this data from Thinking In React and declare PRODUCTS in src/index.js
const PRODUCTS = [
  {category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
  {category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
  {category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
  {category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
  {category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
  {category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
  • In src directory, create file SimpleTable.jsx
import React from 'react';

class SimpleTable extends React.Component {
  render() {
    return (
      <table>
        <tbody>
        </tbody>
      </table>
    )
  }
}

export default SimpleTable;
  • Inside the tbody tags use an array and map() this.props.products to populate the name and price for each product
<tr><td>name</td><td>price</td></tr>


Similar to this:

  <ul>
    {this.props.items.map((value, index) => {
      return <li key={index}>{value}</li>
    })}
  </ul>
  • Modify index.js to render SimpleTable instead of App and pass in products={PRODUCTS} as a prop.
  • Run app in browser
npm start

Lab: Thinking In React Filterable Product Table Catalog

Here's the app you're about to create: Lab Solution Online

create-react-app catalog
const PRODUCTS = [
  {category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
  {category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
  {category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
  {category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
  {category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
  {category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
  • In src directory, create jsx files for each class and add import and export like this:

ProductCategoryRow.jsx

import React from 'react';

class ProductCategoryRow extends React.Component {
...
export default ProductCategoryRow;
  • Import appropriate dependencies for each file like this:
import React from 'react';
import ProductCategoryRow from './ProductCategoryRow';
import ProductRow from './ProductRow';

class ProductTable extends React.Component ...
  • Modify index.js to import FilterableProductTable then render.
    Note that id is 'root', not 'container'
ReactDOM.render(
  <FilterableProductTable products={PRODUCTS} />,
  document.getElementById('root')
);
  • Run app in browser
npm start

Lab: Add State to Filterable Product Table

Here's the app you're about to create: Lab Solution Online


Optional Challenge: Add Cart

Here's the Lab Solution Online

  • Optional Challenge: Create a Cart component and add selected products to the cart
handleAddToCart(product) {
    this.setState({
      cart: this.state.cart.concat(product)  // returns a new array
    })
  }
  • Optional Challenge: use your own test data for real shopping!

Alternative Approach: Refactor to use Functional Classes

Here's the Lab Solution Refactored to Functional Online

Redux Todo Example App from Redux Course

npm install
npm run start
  • Launches on localhost:8080 by default.

Lab: Catalog with React and Redux


More React and Redux Learning Resources


Useful Tools

About

React Dev Labs


Languages

Language:JavaScript 74.4%Language:HTML 22.5%Language:CSS 3.1%