3

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 2

3

Try this one.

const fromCms = {"data-target": "h1", "data-content": "Hello World"}
...
<h1 {...fromCMS}>
Sign up to request clarification or add additional context in comments.

2 Comments

Cannot change the string that's output from CMS
Use regular expression to change it to the JSON string and parse it.
0

You can use the setAttribute() method https://www.w3schools.com/jsref/met_element_setattribute.asp

you must separate the strings to add the atributes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.