Introduction
In this article, you will see how React JavaScript plugins can be plugged in to SharePoint pages for rendering the data.
In my previous article, I explained about the basic terminology required for implementing React JavaScript logic on SharePoint pages.
Note
The core logic is implemented inside the class. The class can be created in two ways:
- React traditionally provides React.createClass method to create the component classes.
- Recently updated React JS version allows us to create the classes, using the React.Component method supported, using ES6 modules.
These two methods differ in implementation approaches. In this article, I will create the components, using ES6 modules.
Steps Involved
Upload the necessary plugins onto SharePoint site libraries. Instead of the local copies, the plugins can be directly accessed from cdn sites.
Create the content editor Webpart manually on the site. The custom Web parts can also be used. Inside the Web part, refer to the plugins explicitly.
- <script src="https://unpkg.com/react@latest/dist/react.js"></script>
- <script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
- <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
Add the root HTML tag for appending the content, using JSX.
- <div id="listInformation"></div>
JSX content is written inside the script tag, which is of the type text/babel. Babel plugin helps the code to be executed on the Browsers.
Create the class with custom name, using React.Component method.
Class should contain a constructor, which has super() method declared. Set the state container with the necessary variables. In this sample, list name, descriptions, template ID and items count are stored.
- constructor(){
- super();
- this.state = {
- listInfo: {listName: '',listDescription: '', templateId:0, itemCount: 0}
- }
- }
Since the data needs to be rendered on the page load, one of the life cycle methods is used to retrieve the list information and set it into the state variables. In this sample, the data is retrieved asynchronously. Thus, componentDidMount method is used. The custom function is created for retrieving the list information.
- GetListInfo(){
- var reactHandler = this;
- var request = new XMLHttpRequest();
- request.open('GET', "/_api/web/lists/getbytitle('TestList')", true);
- request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
- //this callback gets called when the server responds to the ajax call
- request.onreadystatechange = function(){
- if (request.readyState === 4 && request.status === 200){
- var returnedJson = JSON.parse(request.responseText);
- reactHandler.setState({
- listInfo:
- {
- listName: returnedJson.Title,
- listDescription: returnedJson.Description,
- templateId: returnedJson.BaseTemplate,
- itemCount: returnedJson.ItemCount
- }
- });
- }
- else if (request.readyState === 4 && request.status !== 200){
- console.log('Error in retrieving data!');
- }
- };
- request.send();
- }

Vipul JainPosted Mar 19, 2019, 12:51 AM
If someone is facing error with request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');..then use request.setRequestHeader('Accept', 'application/json');
kiranPosted Feb 6, 2019, 5:46 PM
How can we use multiple components in SharePoint Designer
Parthipan SPosted Sep 11, 2018, 12:05 AM
How to use react component like text box, people picker and date picker
Humayun Kabir MamunPosted Feb 12, 2017, 4:00 AM
Thanks for this nice article...