Developing SharePoint Framework Web Parts Using ReactJS - Part Two

Introduction
 
In this article, you will learn about developing a SharePoint framework web part to retrieve and render list items from SharePoint using React JavaScript framework.
 
In my previous article, you will have seen creating the SPFx web part with ReactJS and gotten a basic understanding about the components present.
The following sections show the files of SPFx web part project with necessary changes. The files are already present in the project, only the content should be modified. 
 
Local Properties File (ISplistitemsProps.ts)
 
The properties that will be used across the files are declared in ISplistitemsProps.ts file. The properties are declared using the interface, which can then be accessed by other files. 
  1. export interface ISplistitemsProps {  
  2.   description: string;  
  3.   siteurl: string;  
  4. }  
Note

Don't confuse this file with pane property definition file (ISplistitemsWebPartProps.ts).  
 
Web Part File (SplistitemsWebPart.ts)
 
The web part file is the starting point for rendering the web part data. Let us start the changes with the web part file (SplistitemsWebPart.ts).
  • There are no changes to the properties pane.
  • The render method displays the content.
  • The data should be manipulated using React framework. The same is accomplished with React.createElement. This is the entry point for React library/component. This will create DOM elements. The class and properties are passed as parameters.
  1. public render(): void {  
  2.   const element: React.ReactElement<ISplistitemsProps > = React.createElement(  
  3.     Splistitems,  
  4.     {  
  5.       description: this.properties.description,  
  6.       siteurl: this.context.pageContext.web.absoluteUrl  
  7.     }  
  8.   );  
  9.   
  10.   ReactDom.render(element, this.domElement);  
  11. }   
React Components File (Splistitems.tsx)
 
Modules

Splistitems.tsx file is used for manipulating the React components. Necessary modules are imported using 'import' keyword.
  1. import * as React from 'react';  
  2. import { css } from 'office-ui-fabric-react';  
  3. import styles from './Splistitems.module.scss';  
  4. import { ISplistitemsProps } from './ISplistitemsProps';  
  5. import * as jquery from 'jquery';   
Note
  • Additionally, the jQuery module is imported to retrieve the items as JSON data. The data can be displayed without using jQuery plugin as well.
  • The jQuery plugin is installed using 'npm install --save jquery' and 'npm install --save @types/jquery' commands.
  • Then, it can be imported using "import * as jquery from 'jquery'" in the web part or React component files.
State Object

The list item properties, required for rendering, are declared using the interface (ISplistitemsState). Later, this interface will be used for setting State Object (check constructor method). This interface will be considered as subset of the JSON data (list items, AJAX response,  JSON). 
  1. export interface ISplistitemsState{  
  2.   items:[  
  3.         {  
  4.           "Title":string,  
  5.           "Id":string,  
  6.           "Created":string;  
  7.           "Author":{  
  8.             Title:string;  
  9.           }  
  10.         }]  
  11. }  
Class with constructor and methods
  • The class is defined with the constructor, which has properties and state object parameters. The state object is initialized here.
  • The render method has the HTML elements to be displayed on the web part. Please note here, the state object is used to display the data dynamically on the web part.
  • The list data will be displayed once the state object is loaded. This can be done using any of the ReactJS life cycle methods. In this sample, I have used componentWillMount method to set the state object. The life cycle method pulls the list items using AJAX call as JSON response. On success, the JSON data is initialized to state object.
  1. export default class Splistitems extends React.Component<ISplistitemsProps, ISplistitemsState> {  
  2.   public constructor(props: ISplistitemsProps, state: ISplistitemsState){  
  3.     super(props);  
  4.     this.state = {  
  5.       items: [  
  6.         {  
  7.           "Title""",  
  8.           "Id""",  
  9.           "Created":"",  
  10.           "Author":{  
  11.             "Title":""  
  12.           }  
  13.         }  
  14.       ]  
  15.     };  
  16.   }  
  17.   
  18.   public componentWillMount(){  
  19.     var reactHandler = this;  
  20.     jquery.ajax({  
  21.         url: `${this.props.siteurl}/_api/web/lists/getbytitle('TestList')/items?$select=Title,Id,Created,Author/Title&$expand=Author`,  
  22.         type: "GET",  
  23.         headers:{'Accept''application/json; odata=verbose;'},  
  24.         success: function(resultData) {  
  25.           /*resultData.d.results;*/  
  26.           reactHandler.setState({  
  27.             items: resultData.d.results  
  28.           });  
  29.         },  
  30.         error : function(jqXHR, textStatus, errorThrown) {  
  31.         }  
  32.     });  
  33.   }  
  34.   
  35.   
  36.   public render(): React.ReactElement<ISplistitemsProps> {  
  37.     return (  
  38.       <div className={styles.listItemsForm}>  
  39.         <div className={styles.Table}>  
  40.           <div className={styles.Heading}>  
  41.             <div className={styles.Cell}>Title</div>  
  42.             <div className={styles.Cell}>Created</div>  
  43.             <div className={styles.Cell}>Author</div>  
  44.                   
  45.           </div>  
  46.             {this.state.items.map(function(item,key){  
  47.               return (<div className={styles.Row} key={key}>  
  48.                   <div className={styles.Cell}>{item.Title}</div>  
  49.                   <div className={styles.Cell}>{item.Created}</div>  
  50.                   <div className={styles.Cell}>{item.Author.Title}</div>  
  51.                 </div>);  
  52.             })}  
  53.                   
  54.         </div>  
  55.       </div>  
  56.         
  57.     );  
  58.   }  
  59. }   
Style File (Splistitems.module.scss)
 
The necessary style content is placed inside Splistitems.module.scss file.
  1. .listItemsForm {  
  2.   .container {  
  3.     max-width: 700px;  
  4.     margin: 0px auto;  
  5.     box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);  
  6.   }  
  7.   
  8.   .row {  
  9.     padding: 20px;  
  10.   }  
  11.   
  12.   .listItem {  
  13.     max-width: 715px;  
  14.     margin: 5px auto 5px auto;  
  15.     box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);  
  16.   }  
  17.   
  18.   .button {  
  19.     text-decoration: none;  
  20.   }  
  21.   .listItemTitle{  
  22.     font-size: large;  
  23.     color: darkblue;  
  24.   }  
  25.   .listItemProps{  
  26.     font-size: small;  
  27.   }  
  28.   
  29.   .Table  
  30.     {  
  31.         display: table;  
  32.     }  
  33.     .Title  
  34.     {  
  35.         display: table-caption;  
  36.         text-align: center;  
  37.         font-weight: bold;  
  38.         font-size: larger;  
  39.     }  
  40.     .Heading  
  41.     {  
  42.         display: table-row;  
  43.         font-weight: bold;  
  44.         text-align: center;  
  45.     }  
  46.     .Row  
  47.     {  
  48.         display: table-row;  
  49.     }  
  50.     .Cell  
  51.     {  
  52.         display: table-cell;  
  53.         border: solid;  
  54.         border-width: thin;  
  55.         padding-left: 5px;  
  56.         padding-right: 5px;  
  57.     }  
  58. }   
Test/Deploy
  • Then, the web part can be tested using gulp serve command and then opening the web part using SharePoint site's workbench.aspx page.  
  • The web part can also be packaged and deployed to the site. I have explained about the same in this article
The below snapshot shows the web part added to the site's workbench.aspx page.
 

Summary
 
Thus, you have learned developing the SPFx web part with React framework components to display the list items.