Reader small image

You're reading from  Getting Started with React

Product typeBook
Published inApr 2016
Reading LevelIntermediate
Publisher
ISBN-139781783550579
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Doel Sengupta
Doel Sengupta
author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

Manu Singhal
Manu Singhal
author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

Danillo Corvalan
Danillo Corvalan
author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan

View More author details
Right arrow

Chapter 4. Stateful Components and Events

In this chapter, we will exploit the React states and events in detail. This chapter will cover the components that have a state, the practices to communicate between them, and how to respond to users input/events in order to have the UI reflect this state. We will develop a web page where we can update the name of our liked pages from Facebook. This also covers how the state changes your React UI having enhanced performance using the virtual DOM.

This chapter will cover the following items:

  • React states

  • Event ecosystem

Properties versus states in ReactJS


Let's glance through the differences between props and states in React.

Properties are declared when React components are created, while states are declared within the component definitions. Thus, during the component initialization phase props are declared.

  • In most of the scenarios, React components take data and pass in the form of props. In the other cases, when you are required to take user input to have a server request, states are used.

  • (this.props) is used to access parameters passed from the parent component, while (this.state) is used to manage dynamic data. State should be considered private data.

Exploring the state property


In the last chapter, we explored React properties (props). Just as components can have properties, they can also have states. States are primarily set to those components where it is necessary to change, for example if the component has to be updated or replaced in future. Thus, it is not mandatory to have a state for the entire component.

Components can be both stateless and stateful:

  • Stateless components are those where only props are present, but no state is defined. Thus, there will no change in these prop values for these components within the component life cycle. The only source of static data should be presented to the component instance via the props. Thus, props are immutable data for a React component.

  • Stateful components: Stateless components, are meant to represent any React component declared as a function that has no state and returns the same markup given the same props. As the name implies, stateful components are those where both props and states...

Events


React uses SyntheticEvent, which is a cross-browser wrapper around the browser's native event. So all the event handlers in the react applications will be passed instances of SyntheticEvent. React's event ecosystem has the same interface as any of the browser's native events with the advantage that it works identically in all the browsers and provides stopPropagation() and preventDefault() too.

If React is installed as an NPM module, then these SyntheticEvent-related files can be found in the following location within your app: app/node_modules/react/lib.

All these events comply with the W3C standard. The main event flow happens as:

  • Dispatching the event : @param {object} dispatchConfig

  • Marker identifying the event target: @param {object} dispatchMarker

  • Native event: @param {object} nativeEvent

The way React uses this event delegation is by listening to the nodes that have listeners. Depending on the event handlers on the particular node, the synthetic event system of React implements its...

Summary


According to Facebook's Reconciliation documentation (https://facebook.github.io/react/docs/reconciliation.html)

"React's key design decision is to make the API seem like it re-renders the whole app on every update."

Thus, whenever the setState() method is called on an object, that particular node is marked. At the end of the event loop, all the nodes are re-rendered where the setState() method is called.

React is fast because it never talks to the DOM directly. It maintains an in-memory representation of the actual DOM. Whenever the render() method is called, it returns a mapping of the actual DOM. React can detect (using a diff algorithm) changes in the mapped DOM compared to the in-memory representation. It then re-renders the changes and updates the UI likewise.

The event ecosystem in React is implemented by a full synthetic event system (SyntheticEvent()). Cross-browser efficiency is achieved as all the events bubble up consistently.

In the current chapter, we have explored the stateful...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with React
Published in: Apr 2016Publisher: ISBN-13: 9781783550579
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan