@s-ui/react-head
TypeScript icon, indicating that this package has built-in type declarations

1.13.0 • Public • Published

@s-ui/react-head

Populate the head element of your React app without hassle.

Features:

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

Installation

$ npm install @s-ui/react-head

How to use

Usage

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 = () => (
  <HeadProvider>
    <section className="Home">
      { /* Using tags directly */ }
      <Head>
        <title>Title of page</title>
        <link rel="canonical" content="http://jeremygayed.com/" />
        <meta name="example" content="whatever" />
      </Head>

      { /* Using props */ }
      <Head
        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"
        />
    </section>
  </HeadProvider>
)

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 />
  </HeadProvider>
)
// use headTags to get the content of `<head>`
// and string attributes for `<body>` and `<html>`
const {
  bodyAttributes,
  headString,
  htmlAttributes,
} = renderHeadTagsToString(headTags)

res.send(`
  <!doctype html>
    <html ${htmlAttributes}>
    <head>${headContent}</head>
    <body ${bodyAttributes}>
      <div id="root">${app}</div>
    </body>
  </html>
`)

Readme

Keywords

none

Package Sidebar

Install

npm i @s-ui/react-head

Weekly Downloads

1,836

Version

1.13.0

License

MIT

Unpacked Size

25.1 kB

Total Files

19

Last publish

Collaborators

  • daniel.perez.ext
  • albert.peiro
  • frandelacasa-adevinta
  • mariapaula.forero.ext
  • hpintos_adevinta
  • sziauberyte
  • victor.perez.adevinta
  • oscar-raig-adevinta
  • carlos.gonzalezl
  • carolina.mallo.ext
  • david.nieto
  • ferran.simon
  • xavi_conejo
  • sergi.quintela
  • jamile.radloff
  • xavi_ballestar
  • luz_adv
  • ignacio.rodriguez
  • carlosvillu-adevinta
  • diegomr
  • arturo.vicente
  • adria.velardos
  • emiliovz
  • dann41
  • ruben-martin
  • pol.valls
  • cristina.rodriguez.duque
  • sergio.escano
  • marc.benito
  • azahara
  • patricio.sartore
  • giovanny.sayas.ext
  • david.cuadrado.ext
  • alex.castells
  • beatrizip
  • pablo.rey-adevinta
  • sergiocollado
  • mango.sanchezredondo
  • cristhianb
  • alisa_bayanova
  • davidmartin2108
  • ferrangbtw
  • estefania_garcia
  • belen.santos
  • alfredo.arronte
  • joanleon-adv
  • giodelabarrera.adevinta
  • luis-garrido
  • aitor.rodriguez
  • jordi.munoz
  • oscar_ramirez
  • ignacio_navarro
  • a.ferrer
  • gfabregoadv
  • izeller
  • salvador.juan
  • oriol.egea
  • ivanmlaborda
  • alejandro.ferrante
  • pa.chruscinski.ext
  • isabelgomez87
  • jenifer.lopez
  • daniela.aguilera
  • pablogs
  • javier.miguel
  • oscar.gomez
  • marian.lucaci
  • alverd004
  • oriol.puig
  • nacho_torrella
  • xavi.murcia
  • chloe.leteinturier
  • javiauso
  • alfredo.zimperz
  • francisco.ruiz.lloret
  • fermin.adevinta
  • andresadv
  • schibstedspain