Giter Club home page Giter Club logo

react-hooks-dq-extracting-components's Introduction

Discussion Questions: Extracting Components

Get together in groups and clone down this repository. In the src/App.js file, we have one large component that makes the markup for our entire application. It's your job to refactor this component to several smaller components.

To get started, run the application in the browser:

$ npm install
$ npm start

Start by discussing the demo app and decide what components you need. There's no one right answer, but there are some things you can keep in mind to guide your decisions:

  • Is the same UI repeated multiple times on the page? If so, it makes sense to make a component for that UI.
  • Is one component responsible for too much markup? Think about the idea of separation of concerns - try to break up the responsibility to smaller components.

Once you've decided on your component hierarchy, it's time to start coding!

The React docs have a good guide on Extracting Components.

For each component you decided to make, create a new file and write your component code. Start by hard coding the JSX in the component:

// ResultItem.js
function ResultItem() {
  return (
    <div>
      <h2>Bubby's</h2>
      <p>Price: $$$</p>
      <p>Location: Tribeca</p>
    </div>
  );
}

export default ResultItem;

// App.js
import ResultItem from "./ResultItem";

function App() {
  return (
    <div>
      <ResultItem />
      <ResultItem />
    </div>
  );
}

Then, make it dynamic by passing in props:

// ResultItem.js
function ResultItem(props) {
  return (
    <div>
      <h2>{props.name}</h2>
      <p>Price: {props.price}</p>
      <p>Location: {props.location}</p>
    </div>
  );
}

export default ResultItem;

// App.js
import ResultItem from "./ResultItem";

function App() {
  return (
    <div>
      <ResultItem name="Bubby's" price="$$$" location="Tribeca" />
      <ResultItem name="Nathan's Famous" price="$" location="Coney Island" />
    </div>
  );
}

Once you have your components working, have a look at the src/components/App.js file. You'll notice there are two arrays being imported in this file:

import { messages, contacts } from "../data";

See if you can use these arrays to dynamically generate the contact list and messages for your app instead of hard-coding them!

Check out the React docs on Lists and Keys for a hint.

react-hooks-dq-extracting-components's People

Contributors

graciemcguire avatar ihollander avatar lizbur10 avatar morgvanny avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.