Well, before we start building anything meaningful, its important that we cover some base concepts first, so lets getting start.
What is React?React is a UI library developed at Facebook to facilitate the creation of interactive, stateful and reusable UI components. It is used at Facebook in production, and Instagram.com is written in React.
One of it’s unique points is that not only does it perform on the client side, but it can also be rendered server side, and they can work together inter-operably.
React has quite a small API. This makes it fun to use, easy to learn, and simple to understand. However, being simple does not mean it’s familiar. There are a few concepts to cover before getting started. Let’s look at each in turn:
React elements are JavasScript objects which represent HTML elements. They don’t exist in the browser. They represent browser elements such as an h1, div or section.
JSX is a technique for creating React elements and components.
With a small understanding of these concepts we can move to using React. We will build a series of user interfaces, each adding a layer of functionality on the previous. We will build a photo stream similar to instagram.
The render function accepts two arguments, a virtual element and a real DOM node. React takes the virtual element and inserts it into the given DOM node.
Components are the heart and soul of React. They are custom React elements. They are usually extended with unique functionality and structure.
1 2 3 4 5 6 7
The createClass function accepts an object which implements a render function.
The Photo component is constructed and rendered to the document body.
This component does nothing more than the previous React image element but it’s ready to be extended with custom functionality and structure.
Props can be thought of as a component’s options. They are given as arguments to a component and look exactly like HTML attributes.
1 2 3 4 5 6 7 8 9 10 11 12
Specs, Lifecycle & State
The render method is the only required spec for creating a component, but there are serveral lifecycle methods & specs we can use that are mighty helpful when you actually want your component to do anything.
componentWillMount - Invoked once, on both client & server before rendering occurs.
componentDidMount - Invoked once, only on the client, after rendering occurs.
shouldComponentUpdate - Return value determines whether component should update.
componentWillUnmount - invoked prior to unmounting component.
getInitialState - Return value is the initial value for state.
getDefaultProps - Sets fallback props values if props are not supplied.
mixins - An array of objects, used to extend the current component’s functionality.
The state object is internal to a component. It holds data which can change over time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Having state in a component introduces a bit more complexity.
The component has a new function getInitialState. React calls this function when the component is initialised. The returned object is set as the component’s initial state (as the function name implies).
The component has another new function toggleLiked. This function calls setState on the component which toggles the liked value.
Within the component’s render function a variable buttonClass is assigned either ‘active’ or nothing – depending on the liked state.
buttonClass is used as a class name on the React button element. The button also has an onclick event handler set to the toggleLiked function.
Here’s what happens when the component is rendered to the browser DOM:
- When the component’s button is clicked, toggleLiked is called.
- The liked state is changed.
- React re-renders the component to the virtual DOM.
- The new virtual DOm is compared with previous virtual DOM.
- React isolates what has changed and updates the browser DOM.
Composition means combining smaller components to form a larger whole. For example the Photo component could be used inside a PhotoGallery component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
The Photo component is exactly the same as before.
There’s a new PhotoGallery component which generates Photo components. In this case there’s some fake server data which return an array of 2 objects, each with a url and caption.
The data is looped over and will generates 2 Photo components which are inserted into the return value of the component’s render function.
React also has a built in cross browser events system. The events are attached as properties of components and can trigger methods. Lets make our count increment below using events:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- Clipboard Events -
- Keyboard Events -
- Focus Events -
- Mouse Events -
- Touch events -
- UI Events -
- Form Events -
So far so good, That’s it!!! See ya!!! :)