Giter VIP home page Giter VIP logo

distant-components's Introduction

Distant Components

The goals of this project are to show you sharing data among distant components (in the component tree) is a pain.

Instructions

Scenario

You have an ecommerce website. In the header, you allow users to login with a Login component.

In the footer, you allow users to start a credit card application with a CreditCardApplication component. Users may only apply for a credit card if they are logged in, because we want to provide a personal message for the process: "Hey [name], apply now!"

Therefore, the Login and CreditCardApplication components must communicate or share data. Your task is to pass the data from App.js to the components using built-in React functionality, meaning as props. The challenge is that the components are "distant". The Login component, for example, is in Header, which is in App. So you'll need to pass props, and the update methods to update the state in App.js, to Header, and then in turn pass from Header to Login. When you're done, the app should work.

Here's a visual:

What the app looks like

Here's a more conceptual visual (note that it's taken from a separate video, so doesn't match our scenario exactly):

Conceptual visual of distant components communicating

There is a solution in the solution branch.

Next

Imagine what would happen if there were more components involved, such that the components that need to share data are even more distant. Ew. There more components there are, the greater this pain. Pain here doesn't just mean an annoyance/inconvenience for developers, it also means likelihood for mistakes over time as more values need to be shared.

Solution

There are many solutions here. You could use React's context. But more common is to use a global store solution such as Redux or Mobx.

There is a solution with redux in the with-redux branch.

distant-components's People

Contributors

tylercollier-devmtn avatar

Watchers

James Cloos avatar Aaron Bowers 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.