PnP React Hooks is a wrapper library for PnPjs, provides configurable React hooks for SharePoint Rest API.
Usage:
import * as React from "react";
import { useListItems, useCurrentUser } from "pnp-react-hooks";
export const ExampleComponent = () => {
const currentUser = useCurrentUser();
const items = useListItems("My List", {
query: {
select: ["Title", "Id", "Author/Title"],
expand: ["Author"],
filter: `Author eq ${currentUser?.Id}`
},
disabled: !currentUser
});
return (<ul> { items?.map(item => (<li key={item.Id}>{item.Title}</li>)) } </ul>);
};
npm install pnp-react-hooks
You have to install peer dependencies manually when using older npm versions.
npm install pnp-react-hooks @pnp/sp react
For more details see the docs site.
PnP React Hooks library is an open source project under MIT license. It's NOT an official package distributed by Microsoft.