Understanding React Components On SPFx

Introduction

In this article, let us see how React JS components work along with multiple class hierarchy using SharePoint Framework components.

Web part file 

The TypeScript file present under “src/webparts/solutionname” contains the class and necessary components to render the information. Here, the render method contains the elements to be displayed on the page along with properties passed to the React file.

This element is rendered using ReactDom.render method with the help of React components present in the React.tsx file. The components in the tsx file return the necessary HTML to be rendered on the page.

The below snapshot shows the render method on the main web part file.

  1. public render(): void {  
  2.   const element: React.ReactElement<ISpreactProps > = React.createElement(  
  3.     Spreact,  
  4.     {  
  5.       property1: "Data to Root Class from arg 1",  
  6.       property2: "Data to Root Class from arg 2"  
  7.     }  
  8.   );  
  9.   
  10.   ReactDom.render(element, this.domElement);  
  11. }  

Here, Spreact denotes the main class name present in the React JS file. Any number of parameters can be passed as properties.

Properties file  
 
The properties are declared in the properties file separately (under "src/webparts/solutionname/components" folder). The classes which inherit the property interface should always contain the properties declared in the interface. For example, the interface ISpreactProps contains two properties - property1 and property2. 
  1. export interface ISpreactProps {  
  2.  property1: string;  
  3.  property2: string  
  4. }  
React file
 
Let us look at the React JS file now (under "src/webparts/solutionname/components" folder). The main class contains the render method to return necessary data to be displayed on the web part file. 
  1.  public render(): React.ReactElement<ISpreactProps> {  
  2.   return (  
  3.     <div>  
  4.       <div>  
  5.         Root Class  
  6.       </div>  
  7.       <div>  
  8.         <ClassA property1="data to from root class to Class A" property2={this.props.property2}/>  
  9.         <ClassB property1="data to from root class to Class B" property2=""/>  
  10.       </div>  
  11.     </div>  
  12.   );  
  13. }  
Any number of sub classes can be added here. Additionally, constructor and the life cycle methods can be implemented.
 
In the sample below, ClassB extends the react component and uses the ISpreactProps interface. So, the defined class will contain the properties defined.
  1. <ClassB property1="data to from root class to Class B" property2=""/>  

The properties passed using the classes can be accessed in the respective classes using the props parameter. That is, property1 can be accessed in Class B methods using this.props.property1.

Make sure the React file is imported in the beginning of the file. Likewise, other classes will also contain the constructor, render method, and life cycle methods. The individual classes are used to implement the additional functionalities.

The below sample shows the class. The HTML data is returned to the web part file using render method. The functionalities can be implemented using life cycle methods like componentWillMount, etc.
  1. export class ClassB extends React.Component<ISpreactProps, void> {  
  2.    public constructor(props: ISpreactProps){  
  3.       
  4.     super(props);  
  5.       
  6.   
  7.   }  
  8.           
  9.   public render(): React.ReactElement<ISpreactProps> {  
  10.     return (<div>ClassB</div>);  
  11.   }  
  12. }  

Summary

Thus, you have learned rendering the information using React components with SharePoint Framework solutions.

Up Next
    Ebook Download
    View all
    Learn
    View all