I have a written a class component in SPFx with the pnp/js framework and React.js. I need to transform it to a functional component in react. I'm struggeling to call my SharePoint lists now. I have a SharePoint DataProvider.ts and a pnpConfig.ts
Here ist my code so far: SharePoint DataProvider.ts
import {getSP} from '../PNPConfig/pnpConfig'; import {SPFI} from "@pnp/sp"; import { WebPartContext } from '@microsoft/sp-webpart-base'; import { IMedEvalListItem } from '../spFxReactFuncFragebogen/components/IMedEvalListItemDefinition';} export default class SharePointDataProvider{ private _sp: SPFI; constructor(wpContext: WebPartContext){ this._sp=getSP(wpContext); } public GetSPObject(): SPFI { return this._sp; } public GetLists = async (): Promise<IMedEvalListItem[]> => { try{ const response: IMedEvalListItem[] =await this._sp.web.lists.select("Title","Id").filter("Hidden eq false")(); return response; } catch(e){ return e; } } } PNP Config:
import { WebPartContext } from "@microsoft/sp-webpart-base"; import {spfi, SPFI, SPFx} from "@pnp/sp"; import {LogLevel, PnPLogging} from "@pnp/logging"; import "@pnp/sp/webs"; import "@pnp/sp/lists"; import "@pnp/sp/items"; import "@pnp/sp/batching"; export const getSP= (context:WebPartContext): SPFI =>{ return spfi().using(SPFx(context as WebPartContext)).using(PnPLogging(LogLevel.Warning)); }; My function component:
import * as React from 'react'; //import styles from './SpFxReactFuncFragebogen.module.scss'; import type { ISpFxReactFuncFragebogenProps } from './ISpFxReactFuncFragebogenProps'; import { IMedEvalListItem } from './IMedEvalListItemDefinition'; import SharePointDataProvider from '../../DataProvider/SharePointDataProvider'; export default function SpFxReactFuncFragebogen(props:ISpFxReactFuncFragebogenProps):React.ReactElement<ISpFxReactFuncFragebogenProps> { const[medItems,setMedItems]=React.useState<[IMedEvalListItem]>(); const spService= new SharePointDataProvider(this.context); React.useEffect(() => { fetchListItems(); }, []); const fetchListItems = async () => { try { const items = await spService.GetLists(); setMedItems(items); } catch (error) { console.error('Error fetching list items:', error); } }; return ( <section> <div> {medItems} </div> </section> ); } I'm not sure where my errors are. I need to know if I can call SharePoint like this and how I get the values in the useState Variable.
Best regards
Matthias