Typed Schema.org JSON-LD for React

2.0.1 · active · verified Sun Apr 19

react-schemaorg is a library designed to simplify the integration of structured data into React applications using Schema.org JSON-LD. It leverages the `schema-dts` package for robust TypeScript definitions, ensuring strong type safety when defining rich snippets, which helps prevent common errors in structured data implementation. The current stable version is 2.0.1, with releases typically addressing dependency updates, minor feature enhancements like `@graph` key support, or improved compatibility with popular head management libraries such as Next.js `next/head` and `react-helmet`. Key differentiators include its strong emphasis on type-checking through `schema-dts`, the provision of both a declarative `<JsonLd>` component and utility functions (`jsonLdScriptProps`, `helmetJsonLdProp`) for flexible integration, and its focus on secure JSON-LD parsing to mitigate potential XSS vulnerabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to embed Schema.org JSON-LD data into a React component using the `<JsonLd>` component from `react-schemaorg`. It defines a `Person` schema for 'Grace Hopper' with various properties, showcasing type-safe data structuring for search engines and other semantic web consumers, leveraging TypeScript definitions from `schema-dts`.

import { Person } from "schema-dts";
import { JsonLd } from "react-schemaorg";
import React from 'react';

// Install peer dependencies:
// npm install schema-dts react-schemaorg react

/**
 * A React component demonstrating the embedding of structured data
 * for Grace Hopper using Schema.org's Person type.
 */
export function GraceHopperJsonLd() {
  return (
    <JsonLd<Person>
      item={{
        "@context": "https://schema.org",
        "@type": "Person",
        name: "Grace Hopper",
        alternateName: "Grace Brewster Murray Hopper",
        alumniOf: {
          "@type": "CollegeOrUniversity",
          name: ["Yale University", "Vassar College"],
        },
        knowsAbout: ["Compilers", "Computer Science", "COBOL", "Debugging"],
        url: "https://en.wikipedia.org/wiki/Grace_Hopper",
        sameAs: [
          "https://www.wikidata.org/wiki/Q11652",
          "https://viaf.org/viaf/100384877",
          "https://isni.org/isni/000000011856578X"
        ]
      }}
    />
  );
}

// Example of how to use the component within a larger React application
const App = () => (
  <div>
    <h1>Welcome to My Application</h1>
    <p>This page features an article about a notable figure.</p>
    {/* The JsonLd component will render a script tag in the document head */}
    <GraceHopperJsonLd />
    <footer>
      <p>Structured data is included for SEO purposes.</p>
    </footer>
  </div>
);

export default App;

view raw JSON →