Skip to main content

Managing State in React.js + Flux Applications.

Recently i have been trying out the new kid on the block, React.js and its unidirectional data/state management partner Flux.

The concept of React.js is very simple.

  • Your app is a combination of react components. 
  • Every component has a state and properties.
  • Whenever there is any change in the state or properties of the components the component is rerendered.

Though in the above points i used the state and properties very casually. Usually its very important to get your head around when to use state and when to use properties.  You can refer to this article https://github.com/uberVU/react-guide/blob/master/props-vs-state.md to find out more about props vs state. 

On the other hand Flux which calls itself an architectural pattern rather than a framework, is also straight forward.

  • There is a dispatcher whose main goal is to dispatch the event to all the registered objects.
  • There is a store, the store basically encapsulates the state of a component or group of components. The store registers itself with the dispatcher and receives the events with data from the dispatcher. it can choose which actions it should react to and modify the data it is holding.
  • Actions are sent to the dispatcher with a payload to be distributed to the registered objects.

This example can make it more clear



now a component can listen to this store by registering a listiner and whenever there is a change an event is emitted form the store and the component’s listener method is called back which can update its state.

Comments

Post a Comment

Popular posts from this blog

It was a great experience to talk to a huge audience in Mumbai and Delhi about how to start your ML journey at Google Cloud Summit ’18 India

Fractals and Mandelbrot Set

While mathematics is in itself quite interesting and forms the basis of any modern day research, be it computational biology, machine learning or building complex structure, it can be quite a challenge to decide where to start.  That is why i decided to explore Fractals, thinking of it as a bridge between the nature and science. It brings in some really fascinating concepts which should be good enough for me as a gateway go deeper.  Fractals are in simple language never ending patterns which keep on repeating without an end, because fractals are never ending they have an infinite perimeter but finite area.  Since the patterns repeats indefinitely but if you draw a circle around the peremeter the area will remain finite.  It is like adding 1+0.1+0.01+0.001 and never making 2 This video explains the basic concept really well  Fractals are found everywhere nature in Trees, Rivers, Branching patterns, Hurricanes and Galaxies. It tries to bring order and understanding to the patterns that w