ReasonReact

ReasonReact

  • Docs
  • Try
  • Examples
  • Community
  • Blog
  • Languages iconEnglish
    • 日本語
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • 中文
    • 繁體中文
    • Help Translate
  • GitHub

›Recipes & Snippets

Recipes & Snippets

  • A List of Simple Examples
  • Adding data-* attributes
  • Working with Optional Data
  • Render Props
  • Importing JS into Reason
  • Importing Reason into JS
  • ReasonReact using ReactJS
  • ReactJS using ReasonReact
  • Example Projects
  • GraphQL & Apollo
  • Styling: Tailwind CSS
Edit

Adding data-* attributes

DOM JSX accepts custom data attributes as camelCase props:

let element = <div className="item" dataTestId="item-42" dataState="ready" />;

The rendered element has data-test-id="item-42" and data-state="ready". This works on both HTML and SVG elements.

Values

Data attribute values have type string. They can be literals or expressions:

let itemId = 42;
let element = <div dataItemId={Int.to_string(itemId)} />;

Pass an option(string) with =? to make an attribute optional. Some(value) sets the attribute, and None omits it:

let render = (state: option(string)) => <div dataState=?state />;

let ready = render(Some("ready"));
let absent = render(None);

Attribute names

Uppercase letters become lowercase, with hyphens at word boundaries. Acronyms stay together, digits stay attached to the preceding word, and underscores are preserved:

JSX propDOM attribute
dataTestIddata-test-id
dataURLPathdata-url-path
dataVersion2data-version2
dataFoo_bardata-foo_bar
dataFoo_Bardata-foo_bar
dataFoo__bardata-foo__bar
dataFoo___data-foo___
data_foodata_foo
datafoodatafoo

The standard data and datatype props keep their existing meanings. Other props beginning with data follow the conversion above.

Names containing apostrophes are rejected at compile time because they are invalid DOM attribute names, even when an optional value is None:

/* Rejected: data-foo' is an invalid attribute name. */
let element = <div dataFoo'=?None />;

Apostrophes in attribute values, such as dataLabel="it's ready", are allowed.

Each resulting data attribute name can occur only once on an element. Repeating a prop or using two props that produce the same name is a compile-time error, even when an optional value is None:

/* Rejected: both props render as data-url. */
let element = <div dataURL="first" dataUrl=?None />;

/* Rejected: both props render as data-foo_bar. */
let element = <div dataFoo_bar="first" dataFoo_Bar=?None />;

dataFoo and data_foo are distinct: they produce data-foo and data_foo.

These conversion and validation rules for custom data attribute names also apply in server-reason-react.

Component props

The name conversion applies to DOM elements. Props on your own components keep their declared names and types. Convert values when forwarding them to the DOM:

module Item = {
  [@react.component]
  let make = (~dataId: int) => <div dataId={Int.to_string(dataId)} />;
};

let element = <Item dataId=42 />;

Ordinary DOM props such as className, onClick, and ref retain their usual type checks when used alongside data attributes.

← A List of Simple ExamplesWorking with Optional Data →
  • Values
  • Attribute names
  • Component props