The process involves setting up the development environment, generating a new SPFx project, and building React components that interact with SharePoint data (using REST API ).
React’s state and props manage dynamic data, while SPFx handles the deployment to SharePoint. This combination enables the creation of responsive, data-driven applications in SharePoint.
Here are the steps.
- Install Node.js: Ensure you have the LTS version of Node.js installed, as SPFx projects work best with it.
- Install Yeoman and Gulp
npm install -g yo gulp
- Install SPFx Generator
npm install -g @microsoft/generator-sharepoint
- Create a New SPFx Project
yo @microsoft/sharepoint
- When prompted
- Solution name: Choose your project Integration name.
- Target: SharePoint Online only (latest version).
- Deployment: Choose if you want to deploy to all sites immediately.
- Component type: WebPart.
- Framework: React
- Develop a React Component: Navigate to the components folder inside your web part and create React components.
- Example: Dynamic data rendering in a DynamicComponent.tsx file.
import * as React from 'react';
interface IDynamicComponentProps {
items: string[];
}
const DynamicComponent: React.FC<IDynamicComponentProps> = (props) => {
return (
<div>
<h3>Dynamic List</h3>
<ul>
{props.items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
);
};
export default DynamicComponent;
- Use the React Component in the WebPart: Modify your web part's main file to pass data into the React component. In <YourWebPart>.ts, instantiate your DynamicComponent and pass in any dynamic data, such as from an API or SharePoint list.
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import DynamicComponent from './components/DynamicComponent';
export default class YourWebPart extends BaseClientSideWebPart<{}> {
public render(): void {
const items: string[] = ['Item 1', 'Item 2', 'Item 3']; // Sample dynamic data
const element: React.ReactElement<{}> = React.createElement(
DynamicComponent,
{
items: items
}
);
ReactDom.render(element, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
}
- Run and Test Your App
gulp serve
- Output: When you run the web part in the SharePoint workbench or after deployment, the DynamicComponent will render the list of items passed as props.
- Build and Deploy to SharePoint
gulp bundle --ship
gulp package-solution --ship