Coder Social home page Coder Social logo

react-hooks-information-flow-lab's Introduction

React Information Flow Lab

Learning Goals

  • Use callback functions to update state in a parent component
  • Move state based on which components need access to it

Introduction

Last time we worked on the Shopping List app, we ended up with the following component hierarchy:

App
└── ShoppingList
    ├── Item
    ├── Item
    └── Item

However, it would probably make more sense to break the app down more like this:

demo

App
├── Header
└── ShoppingList
    ├── Filter
    ├── Item
    ├── Item
    └── Item

Your goal is to refactor the code from the previous shopping list lab to match the component structure in the image above. As you work on refactoring the code, keep in mind:

  • Which components should hold state? Which components need access to that state?
  • How can we update state in one component from a child component?

Also, remember what we learned in the previous lesson:

  • For information to propagate down the component tree, parents pass props to their children
  • For information to propagate up the component tree, we typically invoke callbacks that were passed from parents to children as props
  • Components of the same level (sibling components) cannot communicate directly! We can only communicate up and down the component tree. So if multiple components need to share the same information, that state should live in the parent component (or a more general ancestor).

Deliverables

Header

Create a Header component by refactoring the <header> element out of the App component. Clicking on the <button> inside of the Header component should still toggle dark mode on and off.

You will need to pass a callback function as a prop called onDarkModeClick to the Header component in order for the test to pass.

Filter

Create a Filter component by refactoring the <select> element out of the ShoppingList component. Changing the selection in the dropdown should still change which items are displayed in the shopping list.

You will need to pass a callback function as a prop called onCategoryChange to the Filter component in order for the test to pass.

Resources

react-hooks-information-flow-lab's People

Contributors

ihollander avatar lizbur10 avatar annomao 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.