Skip to content
guptapellucid edited this page Jul 22, 2014 · 33 revisions

React - A library for creating user interfaces.

React is about V in MVC

- It is more of a library than a framework.
- Can be used with any other framework / technology.

Motivations for React

- Templates are underpowered. 
- Keeping UI in sync with ever changing state changes is a hard problem.

What's wrong with templates ?

- Nothing. They served the purpose of generating DOM by combining data and markup in a clean way.
- But they are limiting:
    - Display logic is separated into templates and view models.        
    - Templates represents the state of the UI at the beginning. Afterwards, we are on our own.
    - Do not use the full power of javascript (#each, #if like constructs are limiting).
- We can do better.

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.

React's implementation

  - Re-render UI whenever data changes. 
  - Virtual DOM
  - Synthetic Events 
  - Batch Rendering

ReactComponent

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

Simple React Component

state vs props

- React components are basically state machines.
- 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.
- A common pattern is to create several stateless components that just render data, and have a stateful component above them in the hierarchy that passes its state to its children via props. The stateful component encapsulates all of the interaction logic, while the stateless components take care of rendering data in a declarative way.
- These things shouldn't go into state
    - Computed values based on other state variables.
    - Duplicated data from props. 

Composite React Component

Communication between components

Composite Keys and reconciliation of nodes

Life cycle of a React component

References

Clone this wiki locally