Skip to content
guptag edited this page Apr 1, 2015 · 33 revisions

React - A library for creating user interfaces.

React is about V in MVC

- React is not a MVC framework. It doesn't come with controllers, router etc.
- React is all about Views in the application.
- Can be used with any other framework / technology.

Motivations for React

- Templates are underpowered.
  - Templates greatly served the purpose of generating HTML by combining data 
    and markup in a clean way. But they are limiting in ways.
  - Templates represent the state of the UI at the beginning. Afterwards, we are on our own.      
  - UI and display logic is separated into templates and view models.
  - Templates do not use the full power of javascript (#each, #if like constructs are limiting).

- Dom mutations are slower
  - The slowest part of the web application is dom updates.

- Building user interfaces is hard.
   - Lots of state.
   - Random events (user actions, ajax callbacks, timer events) can change state in different timeframes.
   - Keeping UI in sync with ever changing state often results in spaghetti 
     and hard to debug code.

ReactComponent

- React approaches building user interfaces by breaking them into components.     
- Combines DOM representation and display logic into one unit.
- React components describes UI at any point of time.
- Encourages loose coupling with other components.
- Components are composable and reusable.
- Radical shift from thinking in templates and view models.

React's implementation

  - Re-render UI whenever data changes. 
  - Virtual DOM
      - Javascript is super fast; dom mutations are the slowest part in the web applications.
      - Whenever state changes, React recreates virtual dom and compares with the 

existing virtual dom. With intelligent heuristics it figures out the minimal changes needed to DOM and queues those changes for batch update. - React's philosophy of Re-render everytime when data changes was made possible because of the underlying virtual dom implementation. - Synthetic Events - Diff Algorithm - Level by Level - Component Matching - Batch Rendering - Sub-tree rendering

Simple React Component

state vs props

- props
  - Props are inputs set on a components by its parent and cannot be changed within the component.

- state
  - State is created within the component and can be changed.
  - State should contain data that a component's event handlers may change to trigger a UI update.
  - State shouldn't contain computed data from other variables, duplicated data from prop.
  - Try to keep as many of your components as possible stateless. 

By doing this you'll isolate the state to its most logical place and minimize redundancy, making it easier to reason about your application.

References

Clone this wiki locally