Giter Site home page Giter Site logo

doc-js-intro-to-components-and-states's Introduction

Components, states, and interoperation among them

React allows the development of components in hierarchies. As an example, a main window frame can have a window inside. And inside a window you can have a bird:

<windowFrame>
  <windowSpace>
    <bird />
  </windowSpace>
</windowFrame>

Because life is life, all these elements have attributes, the window have frame, for example, have color. The window space have size, and the bird is hungry so the bird can eventually become bigger in size. These attributes can be considered states and it's easy to see them in isolation. The bird's state, the windowSpace state and the windowFrame state.

When looking at the states in isolation it's all very simple but the problem with that is that it does not work like real life. That because in reality the states of one element may impact or relate to the state of other element. As an example, if you feed the bird too much, it will grow in size and it may not fit the window space anymore.

In order to implement a solution that works more like real life, we need states to "talk" each other. In the more traditional paradigm of programming, the solution to the problem could be as easy as to put a conditional in the parent component, for example to have the windowSpace to check the bird's size state, and then if the bird' size is too big, the window can ask the bird to leave; or the other way around which is to have the bird check the size of the space and if is too tight then the bird leaves.

The decision on which component to have the conditional check is usually a developer's decision based in demands of the problem. It would also be common situation for both components to negotiate. Say the bird wants to know the size available but also the windowSpace have certain rules, such as to always allow space for a new bird.

Why and how to conciliate component hierarchies with shared states

doc-js-intro-to-components-and-states's People

Contributors

taboca avatar

Watchers

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