Implementing SharePoint Operations Using React.js - Part One

Introduction
 
In this article series, you will learn how to implement basic SharePoint operations using React JavaScript libraries.
 
In this article, you will learn the React JavaScript library basics required for implementing SharePoint operations.
 
ReactJS
  • React is a front-end library developed and licensed by Facebook.
  • React is a declarative, efficient, and flexible JavaScript library for building user interfaces.
  • It is famous for 'V' in MVC, which handles view layer on the web.
  • Using ReactJS, we can build reusable components. 
There are multiple ways of implementing the React JavaScript logic into SharePoint. In this article, I will use the React JavaScript libraries stored on the web. Additionally, Babel compiler is required for compiling the JSX content.
 
The library files referenced are,
  • react.js
  • react-dom.js
  • babel.min.js 
JSX
 
The core functionality is written on JSX. Here, Babel compiler is used to transform the JSX content to JS.
  • JSX content is embedded with in the script tag, which is of the type text/babel.
  • JSX contains JavaScript with HTML/XML in it.
  • JSX is faster, type safe and object-oriented programming language. 
  • JSX is not the globally accepted standard, so it is not supported on browsers.
Some of the React JavaScript basics used for our implementation are explained below. 
  1. Components
    Components help us split the code/UI in multiple pieces. The component contains a constructor and methods.

  2. State
    State stores the data. The data can be in multiple forms. The data can be a string, number, array, etc.

  3. Props
    Props are attributes defined in the components. These are immutable and passed as arguments for rendering the HTML. propTypes are used for validating the props.

  4. Render
    It is a method, which returns the HTML content that needs to be displayed on the page.

  5. Constructor
    Constructor declares the variables, handlers and the methods.

  6. Component Life cycle
    There are predefined set of methods executed when the component is executed. The life cycle methods are componentWillMount, componentDidMount, componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, componentDidUpdate, and componentWillUnmount. 
The React JavaScript library basics can be found on the official site https://facebook.github.io/react.
 
Prerequisites
 
In the sample, I will refer the React JavaScript and Babel plugins directly on the content editor webpart.
The sample JSX content will look like below.
  1. class Sample extends React.Component {  
  2.     constructor(){  
  3.         super();  
  4.         this.state = {  
  5.             data:''  
  6.         }  
  7.     }  
  8.     componentWillMount(){  
  9.     }  
  10.     componentDidMount() {  
  11.     }  
  12.     // Other Life Cycle Methods  
  13.   
  14.     render(){  
  15.         return(  
  16.             <div>  
  17.                 HTML  
  18.             </div>  
  19.         );  
  20.     }  
  21. }  
  22. ReactDOM.render(<Sample />, document.getElementById('divid'));   
Summary
  
Thus, you have learned the basics of React JavaScript library to be implemented for SharePoint operations. In my next article, I will explain how to implement the SharePoint functionalities using React JavaScript libraries.