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

react-contentful-update

v1.4.6-sai

Published

React library for interacting with and rendering Contenful data.

Downloads

57

Readme

📰 react-contentful-update

npm version npm Coverage Status CircleCI Greenkeeper badge

A React component library that makes it super simple to compose Contentful content into your sites and applications.

Install

Via npm

npm install react-contentful-update

Via Yarn

yarn add react-contentful-update

Why this package is created

This is a modify version of react-contentful to be able use only query in <Query /> component which I've made pull request but still pending so for the use of urgent case in my project I uploaded to npm. This is necessary in getting mutiple content types .I gave full credit to react-contenful.

Query

In this example, the Query component accepts a query parameter that filters Page content types from Contentful based on the slug field set on published Page content models.

import React from 'react';
import { Query } from 'react-contentful';

const Page = (props) => (
  <Query
    contentType="Page"
    query={{
      'fields.slug[in]': `/${props.match.slug || ''}`,
    }}
  >
    {({data, error, fetched, loading}) => {
      if (loading || !fetched) {
        return null;
      }

      if (error) {
        console.error(error);
        return null;
      }

      if (!data) {
        return <p>Page does not exist.</p>;
      }

      // See the Contentful query response
      console.debug(data);

      // Process and pass in the loaded `data` necessary for your page or child components.
      return (
        ...
      );
    }}
  </Query>
);

export default Page;

Query component with multiple content type

In this example, the Query component accepts a query parameter that accept query props to request mutliple content type.

import React from 'react';
import { Query } from 'react-contentful';

const Page = (props) => (
  <Query
    contentType="Page"
    query={{
      'sys.contentType.sys.id[in]': 'contentType-1,contentType-2',
    }}
  >
    {({data, error, fetched, loading}) => {
      if (loading || !fetched) {
        return null;
      }

      if (error) {
        console.error(error);
        return null;
      }

      if (!data) {
        return <p>Page does not exist.</p>;
      }

      // See the Contentful query response
      console.debug(data);

      // Process and pass in the loaded `data` necessary for your page or child components.
      return (
        ...
      );
    }}
  </Query>
);

export default Page;

Components

Below are the following components and classes that are availabe in this package that makes it easy to integrate Contentful into your site or application.

ContentfulProvider

Provider that offers accesss to a centralized ContentfulClient that not only can make all your Contentful requests, but also handles caching those requests during your session to keep things optimized and fast.

| Prop | Default | Description | | ---------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | client | null | Required for children that utilize withContentful to make requests to Contentful. | | locale | en-US | Default locale to use for requests against the Contentful API. | | renderPromises | null | Not used during normal use, but utilized by other libraries like next-content for use during server-side rendering. |

ContentfulClient

Instance of Contentful client that is for making requests and caching responses.

| Options | Default | Description | | ------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------- | | space | null | ID of the Contentful space that queries will be submitted to. | | accessToken | null | Access token used for client initialization. | | host | cdn.contentful.com | Host to use for requests. Accepts either, cdn.contentful.com or preview.contentful.com. | | cache | new ContentfulCache() | Cache used for caching responses during a session, as well as rehydrating the client/app when used during server-side rendering. | | ssrMode | false | Flag to specify when client is being used during server-side rendering. |

The ContentfulClient is an extension of the Contentful Delivery API SDK. For more information about what options are available when creating a client, along with other useful insights, check out the Official Contentful documentation.

ContentfulCache

Cache instance used for caching responses in memory during a session, along with building up a cache of responses for responses used to render/rehydrate the app when used during server-side rendering. You would typically not have to work with this class directly, unless you are rolling your own server-side rendering solution or have some ideas around warming the cache. Otherwise, you should check out next-contentful if you’re working on a React/Node/Express app.

| Arguments | Default | Description | | --------- | ------- | -------------------------------------------------- | | cache | null | Initializes a new Map instance to use for cache. |

Query

This is where the magic happens. You can compose Query wherever you need to reference or conditionally render content based on Contentful data. Querys can be used standalone, or to wrap content that is reliant on the data.

| Props | Default | Description | | ------------- | ------------------------------------------- | ----------- | | contentType | null | Content type associated with the content model that you are querying for within Contentful. Required for non-id queries. Results in an array of results returned. | | id | null | Entry id associated with the content model in Contentful. Returns a single data model from Contentful if it exists. | | include | 10 | Depth of referenced content to include in the query. Defaults to 10. | | query | null | Query object used for defining the search parameters to use for the request. You can reference all available options via Contentful official documentation | | parser | (data, props) => data | Parser to use for manipulating the response data before being pass to the children/returned via callbacks. | | skip | false | Flag used to skip the Query instance when being referenced during server-side rendering. | | onError | ({ data, error, fetched, loading }) => {} | Callback for when an error is encountered during the request. fetched will be set to true and error will be set. | | onLoad | ({ data, error, fetched, loading }) => {} | Callback for when the response has completed. fetched will be set to true and data will be set. | | onRequest | ({ data, error, fetched, loading }) => {} | Callback for when the request has been initiated. loading will be set to true and all other values will be null or false. |

withContentful

Higher-order component that is available in case you want to build your own Contentful ready components. Used by the Query component for providing access to the ContentfulContext.

import { withContentful } from 'react-contentful';

const YourComponent = ({ contentful }) => {
  const { client, locale, renderPromises,  } = contentful;

  return (
    ...
  );
};

export default withContentful(YourComponent);

Using Next.js?

If you like what you see above, you might like next-contentful, which lets you easily add react-contentful to your Next.js app. Making it easy to ensure that all your Query instances render awesomely server-side.

License

MIT © Ryan Hefner