> Populate the head element of your React app without hassle.

Downloads in past


1.12.02 months ago3 years agoMinified + gzip package size for @s-ui/react-head in KB


Populate the head element of your React app without hassle.

  • Change the <head> content by using React components.
  • Add attributes to <body> and <html> tags.


$ npm install @s-ui/react-head

How to use


Using <Head> component will allow you to define the tags inside the <head> element. You could use two different ways: using the children of the component with the desired tags or using the props.
import Head, { HeadProvider } from '@s-ui/react-head'

const App = () => (
    <section className="Home">
      { /* Using tags directly */ }
        <title>Title of page</title>
        <link rel="canonical" content="http://jeremygayed.com/" />
        <meta name="example" content="whatever" />

      { /* Using props */ }
        bodyAttributes={{ class: 'search full-Width' }}
        htmlAttributes={{ lang: 'es-ES' }}
        link={[{ rel: 'canonical', href:'https://canonical.com' }]}
        meta={[{ name: 'description', content: 'The description' }]}
        title="Title of page"

Important ⚠️: You can't mix both of them in order to define link, meta and title tags. The children will have precedence and overwrite if you try to combine both. However, you could still use htmlAttributes and bodyAttributes with the children approach.

Server Side Setup

import {HeadProvider} from '@s-ui/react-head'
import {renderHeadTagsToString} from '@s-ui/react-head/lib/server'
import {renderToString} from 'react-dom/server'
// headTags will be mutated, so you need to create a new variable
// for each request to avoid collisions
const headTags = []
const app = renderToString(
  <HeadProvider headTags={headTags}>
    <App />
// use headTags to get the content of `<head>`
// and string attributes for `<body>` and `<html>`
const {
} = renderHeadTagsToString(headTags)

  <!doctype html>
    <html ${htmlAttributes}>
    <body ${bodyAttributes}>
      <div id="root">${app}</div>