Coder Social home page Coder Social logo

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

Discussion Questions: Props and Component Reusability

Welcome to Ye Olde Apothecary!

Objectives

This discussion question is focused on using props to make our components more reusable. In order to do so, we have to adopt a mindset around abstraction, meaning that we should keep an eye out for similarities in our components and figure out clever ways to use props to abstract out the differences.

Overview

This discussion question comes with a fully functioning application. To get going, simply:

npm install && npm start

Feel free to play around with it and see how it works. As you do, notice that the tiles that render inside the shop and inside the inventory look suspiciously similar, but have different functionality and slightly different styling.

Your goal is to write a new component called PotionTile (basic template provided for you) that will act as an abstracted version of InventoryTile and ShopTile. If done correctly, you should be able to replace these components wherever they are written with your new PotionTile and the application should retain both its styling and its functionality.

Note: Don't worry too much about what the rest of the code is doing, especially addToInventory and sell. These functions take potion IDs as arguments, and so long as you do so, they should behave normally. All of your new code should be in PotionTile with slight modifications to Shop and Inventory.

Hint: Don't be afraid to modify the props that are passed to the tiles in Shop and Inventory!

Note on Children

Inside both InventoryTile and ShopTile, you'll see something called children, even though we didn't pass a prop called children to either one. This is a cool feature of React that allows your components to have children just like HTML elements can have children! Consider the following example:

// In HTML, you can have children by placing an element between opening in closing tags

<div>
  <h1>Child</h1>
  <h1>Another Child</h1>
</div>

// In JSX, you can do the same with your own components!

<MyComponent>
  <h1>Child</h1>
  <h1>Another Child</h1>
  <MyChildComponent />
</MyComponent>

In the above example, MyComponent will receive a prop called children, an array containing 3 components: 2 h1 and the MyChildComponent in that order. We can then render this array just like any other array of components.

For further reading, take a look at this resource: JSX Children

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

Contributors

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