Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@researchgate/react-structured-data

researchgate147MIT1.2.0

Declarative JSON-LD Structured Data for ReactJS Apps

react, React, react-jsonld, json-ld, JSON-LD, jsonld, JSONLD, schema.org, schema, structured data, seo, react-component

readme

React Structured Data

React Structured Data provides an easy way to add structured data to your React apps. Structured data is a standardized format for providing information about a page and classifying the page content. This library follows guidelines specified at http://schema.org/.

Installation

Yarn

yarn add @researchgate/react-structured-data

NPM

npm install @researchgate/react-structured-data --save

Code Example

The following JSX:

<JSONLD>
  <Product name="Product Name">
    <AggregateRating ratingValue={4.3} reviewCount={197} />
    <GenericNodeCollection type="review">
      <Review
        name="It's awesome"
        reviewBody="This is Great! My family loves it"
        datePublished="11/22/1963"
      >
        <Author name="Jerry" />
        <Location name="Chicago, IL" />
        <Rating ratingValue={5} />
      </Review>
      <Review
        name="Very cool"
        reviewBody="I like this a lot. Very cool product"
        datePublished="11/22/1963"
      >
        <Author name="Cool Carl" />
        <Location name="Chicago, IL" />
        <Rating ratingValue={4} />
      </Review>
    </GenericCollection>
  </Product>
</JSONLD>

will add the following to your markup (will be minified):

<script type="application/ld+json">
  {
    "@context": "https://schema.org/",
    "@type": "Product",
    "name": "Product Name",
    "aggregateRating": {
      "@type": "AggregateRating",
      "ratingValue": 4.3,
      "reviewCount": 197
    },
    "review": [
      {
        "@type": "Review",
        "datePublished": "11/22/1963",
        "reviewBody": "This is Great! My family loves it",
        "name": "It's awesome",
        "author": {
          "@type": "Person",
          "name": "Jerry"
        },
        "locationCreated": {
          "@type": "AdministrativeArea",
          "name": "Chicago, IL"
        },
        "reviewRating": {
          "@type": "Rating",
          "ratingValue": 5
        }
      },
      {
        "@type": "Review",
        "datePublished": "11/22/1963",
        "reviewBody": "I like this a lot. Very cool product",
        "name": "Very cool",
        "author": {
          "@type": "Person",
          "name": "Cool Carl"
        },
        "locationCreated": {
          "@type": "AdministrativeArea",
          "name": "Chicago, IL"
        },
        "reviewRating": {
          "@type": "Rating",
          "ratingValue": 4
        }
      }
    ]
  }
</script>

Reference

PropTypes

Generic Component PropTypes

PropType Value Description
type String The @type description in the json-ld body: "@type": "Product"
jsonldtype String The value of the @type description in the json-ld body: "@type": "Product"
schema Object (json) This should be the schema that you want for your structured data node: {name: "It is awesome", reviewBody: "This is great!"}

Schema node PropTypes

PropType Value Description
id String similar to parentID but uses the ID on itself

Preset Components

There are several preset schema components that can be used

  • AggregateRating
  • Answer
  • Author
  • ItemReviewed
  • Location
  • Product
  • Question
  • Rating
  • Review
  • QAPage
  • Organisation
  • Person
  • Review
  • Place

If you would like to use a component that is not listed, simply use the Generic component and add the prop type. Generic and GenericCollection allow you to add your own structured data type.

For example, If Review preset didn't exist, you could write:

<JSONLD>
  <GenericNode
    type="review"
    jsonldtype="Review"
    schema={{ name: 'It is awesome', reviewBody: 'This is great!' }}
  >
    <GenericNode
      type="itemReviewed"
      jsonldtype="Product"
      schema={{ '@id': 'product-x' }}
    />
    <GenericNode
      type="author"
      jsonldtype="Person"
      schema={{ name: 'Cool Carl' }}
    />
    <GenericNode
      type="locationCreated"
      jsonldtype="AdministrativeArea"
      schema={{ name: 'Chicago, IL' }}
    />
  </GenericNode>
</JSONLD>

This will output (minified):

<script type="application/ld+json">
  {
    "@context": "http://schema.org/",
    "@type": "Review",
    "name": "It is awesome",
    "reviewBody": "This is great!",
    "itemReviewed": {
      "@type": "Product",
      "@id": "product-x"
    },
    "author": {
      "@type": "Person",
      "name": "Cool Carl"
    },
    "locationCreated": {
      "@type": "AdministrativeArea",
      "name": "Chicago, IL"
    }
  }
</script>

This may seem not as ideal as using the presets, but this allows completely customizable structured data. There will also be more preset components to come in future releases to make implementation easier so stay tuned!

Structured Data and Schema.org

For more information on Structured data, visit https://developers.google.com/search/docs/guides/intro-structured-data, and also http://schema.org/. You can also validate the structured data here: https://search.google.com/structured-data/testing-tool.

Contributors

TBA

License

MIT License

changelog

1.2.0 (2020-09-23)

Features

  • export generic nodes for ad-hoc schema implementations (16f4ef4)

1.1.0 (2019-08-27)

Features

  • dangerouslySetInnerHTML property is removed. All the string props will get encoded and dangerouslySetInnerHTML will be used (19da0b8)

1.0.6 (2019-08-26)

Bug Fixes

  • empty collections will render to null (6bc868d)

1.0.5 (2019-08-26)

Bug Fixes

  • filtering properties that have null as a value (b938aee)

1.0.4 (2019-08-26)

Bug Fixes

  • filtering of null and [secure] nodes from a collection (a9213fc)

1.0.3 (2019-08-26)

Bug Fixes

  • added .npmrc to gitignore (daf5fb4)

1.0.2 (2019-08-26)

Bug Fixes

1.0.1 (2019-08-23)

Bug Fixes

  • added publish config so semantic-release will release into public package on npm (66086dd)

1.0.0 (2019-08-23)

  • added new schema.org types and added tests (6ea022c)

BREAKING CHANGES

  • code was rearranged