Coder Social home page Coder Social logo

react-information-flow's Introduction

Objectives

  • Practice enforcing React up-down information flow
  • Practice debugging

React component structure

As you already know, a React application consists of many [components][components-readme]. Components have a 'parent'/'child' relationship that facilitates rendering order. While a component may have many children, it should only have one parent. Remember: components receive their props and own their state:

  • When props are passed from a parent to a child, they are meant to be unmodified. If a child needs a different prop, it should come from its parent
  • state is meant to be changed within a component using this.setState()

React information flow

When developing in vanilla React, we adhere to the following data flow paradigm:

  • For information to propogate 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

Getting Started

Take a moment to familiarize yourself with the code base. We have a simple application that renders 3 unique components: Tier1, Tier2, and Tier3. Following is what our component structure looks like when fully rendered:

App
├───Tier1
│   ├───Tier2
│   │   └───Tier3
│   │   └───Tier3
│   └───Tier2
│       └───Tier3
│       └───Tier3
└───Tier1
    ├───Tier2
    │   └───Tier3
    │   └───Tier3
    └───Tier2
        └───Tier3
        └───Tier3

src/randomColorGenerator.js has a few helper functions implemented for you that generate random colors, as well as lighter tints of a specific color.

Deliverables

Understanding that React components should communicate up and down the component tree, we want to make sure that the following behavior is exhibited:

  • When a Tier component is clicked it, and all other components within the same parent, should change colors (i.e. If a Tier2 component is clicked, the other Tier2 within the same parent component should also change color)
  • When a component's color changes all of its children components should change to a lighter shade of the same color (use the imported getReducedColor function)
  • Components of the same level should never communicate directly! For example, when a tier3 component is clicked, it should communicate this up the component tree (i.e. via a callback method that was passed).

The color changing functionality itself has already been implemented -- while you should feel free to, there is no need to alter the existing code in src/randomColorGenerator.js

A lot of starter code has been provided. Ultimately, you should only need to make minimal changes. While the React application is rendering without any bugs, not all of the provided code in our Tier1 and Tier2 components is what we want it to be! Be critical and make changes where appropriate.

Example

The gif below shows what a fully working application should look like. While you won't be generating identical colors, the functionality should be the same:

You will notice that even if children components change colors without their parents, a changing of their parents' colors resets them.

Refactoring

Several different solution branches have been provided. Please complete your own solution before taking a look at them:

  • Did your's match any of the same patterns, or was it a novel solution?
  • How could we make our application more DRY?

react-information-flow's People

Contributors

danielseehausen avatar thuyanduong-flatiron avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mutheulinet

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.