Giter Site home page Giter Site logo

data-dump's Introduction

Props

In this activity, we will modify a React application to make it more DRY through the use of props.

Instructions

  • Replace your Create React App's src folder with the provided src folder.

  • Stop the dev server if it is already running. Start the app in dev mode by running yarn start. Open localhost:3000 in your web browser and take a moment to study the rendered app.

    Unfinished Diagram

    • This application is the start of a friends list for a social network.
  • Open src/App.js in your editor and notice how the SpongebobCard, MrKrabsCard, and SquidwardCard are being rendered inside of Wrapper, which is in turn being rendered inside of the App component.

  • Open up each component's files in turn and briefly study the JSX being returned by each. Most importantly, notice how all of the card components render very similar JSX. This isn't very DRY code. We'll fix this by creating a brand new FriendCard component and rendering it once for each friend. We'll pass down the differentiating content in the form of props.

    Finished Diagram

  • Take a moment to study the src/friends.json file. This file contains the data that makes each friend card unique from one another. We'll use this file to pass props down to our new FriendCard component.

  • Import the src/friends.json file into src/App.js. This can be achieved with the following syntax:

    import friends from "./friends.json";
  • Inside of src/App.js, remove the imports for each friend's card, and remove the JSX rendering each card inside of this file.

  • Create a brand new component named FriendCard.

    • The FriendCard component can initially return the same JSX as SpongebobCard, and have its own CSS file named FriendCard.css which should have the same contents as SpongbobCard.css.

    • Require the FriendCard component inside of src/App.js and render it inside of the Wrapper component. Before going any further, check your browser. If successful, you should only see one card being rendered.

  • Still inside of src/App.js, use the first friend object in the friends JSON file to pass the following props down to your rendered FriendCard component.

    • name

    • image

    • occupation

    • location

  • Modify the FriendCard component so that it accepts and renders all of the passed props in place of the currently hard coded values. Once complete, check your browser to make sure the first FriendCard is still being properly rendered.

  • Inside of src/App.js, render another FriendCard component for the second and third piece of friend data. Pass down the appropriate JSON data for each as props. If successful, you should see each friend being rendered to the browser, utilizing the same same FriendCard component three times.

Hints

data-dump's People

Contributors

throwback74 avatar

Watchers

 avatar  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.