npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@contentful/react-apps-toolkit

v1.2.16

Published

Toolkit for building a Contentful app in React

Downloads

62,280

Readme

React Toolkit for Contentful Apps

React Hooks for the App Framework offer a simple way to bring frequently needed functionality into your react based Contentful apps.

They can be used in apps created with create-contentful-app, as well as any other React app using Contentful's App SDK.

Installation

npm install @contentful/react-apps-toolkit
# or
yarn add @contentful/react-apps-toolkit

Available features

The following hooks and utilities are exported from the package:

SDKProvider

The SDKProvider is a wrapper component, which automatically makes the Contentful App SDK available to any child components using React Context. To use any of the hooks contained in this package, they must be wrapped in the <SDKProvider>, because all of the hooks depend on the App SDK.

Usage:

import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';

function ChildComponentUsingHook() {
  const sdk = useSDK<FieldExtensionSDK>();

  return <>App Id: {sdk.ids.app}</>;
}

function App() {
  return (
    <SDKProvider>
      <ChildComponentUsingHook />
    </SDKProvider>
  );
}

useSDK

useSDK returns an instance of the Contentful App SDK.

It must be wrapped it within the SDKProvider, otherwise, it will throw an error.

Usage:

import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';

function ComponentUsingSDK() {
  const sdk = useSDK<FieldExtensionSDK>();

  return <>App Id: {sdk.ids.app}</>;
}

function App() {
  return (
    <SDKProvider>
      <ChildComponentUsingSDK />
    </SDKProvider>
  );
}

useCMA

:warning: DEPRECATED If you are using App SDK v4.20 or greater use sdk.cma instead.

useCMA returns an initialized client for the Contentful Management API. This can be used immediately to communicate with the environment the app is rendered in. Contentful Management API docs.

Note: The CMA client instance returned by this hook is automatically scoped to the contentful space and environment in which it is called.

Usage:

import { SDKProvider, useCMA } from '@contentful/react-apps-toolkit';

function ComponentUsingCMA() {
  const cma = useCMA();
  const [entries, setEntries] = useState();

  useEffect(() => {
    cma.entry.getMany().then((data) => setEntries(data.items));
  }, [cma]);

  return <>{entries?.length}</>;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingCMA />
    </SDKProvider>
  );
}

useFieldValue

useFieldValue provides the current value, and a setter function for updating the current value, of a given field in Contentful. If used in the field location, it will initialize using the current field id by default.

If used in the entry sidebar location, or the entry editor location, it must be passed a field ID to initialize.

useFieldValue also optionally accepts a locale, if the field has multiple locales. If no locale is passed, it will use the environment's default locale.

Usage:

import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';

function ComponentUsingFieldValue() {
  const [value, setValue] = useFieldValue('slug', 'en-US');

  return <input value={value} onChange={(e) => setValue(e.target.value)} />;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingFieldValue />
    </SDKProvider>
  );
}

useAutoResizer

useAutoResizer listens for DOM changes and updates the app's height when the size changes.

Usage:

import { SDKProvider, useAutoResizer } from '@contentful/react-apps-toolkit';

function ComponentUsingAutoResizer() {
  useAutoResizer();

  return <div>Component will be auto-resized</div>;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingAutoResizer />
    </SDKProvider>
  );
}

Resources