Wednesday 14 December 2016

Implementing SharePoint Operations Using React.js - Part One

In this series, you will learn how to implement basic SharePoint operations using React JavaScript libraries.

Here, 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. ComponentsComponents help us split the code/UI in multiple pieces. The component contains a constructor and methods.
  2. StateState stores the data. The data can be in multiple forms. The data can be a string, number, array, etc.
  3. PropsProps 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. RenderIt is a method, which returns the HTML content that needs to be displayed on the page.
  5. ConstructorConstructor declares the variables, handlers and the methods.
  6. Component Life cycleThere 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'));