Add attributes to React element from string

Viewed 188

My CMS outputs a string of data attributes to add to an element I wanted to know how I can add the string of attributes to an element in React

This sample code does not appear to work.

function HelloWorld(props) {
  const fromCMS = `data-target="h1" data-content="Hello World"`;
  return (
    <h1 {fromCMS}>{props.content}</h1>
  )
}
2 Answers

Try this one.

const fromCms = {"data-target": "h1", "data-content": "Hello World"}
...
<h1 {...fromCMS}>
Related