Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attributes to React element from string

Tags:

reactjs

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>
  )
}
like image 401
henryaaron Avatar asked Feb 06 '26 12:02

henryaaron


1 Answers

Try this one.

const fromCms = {"data-target": "h1", "data-content": "Hello World"}
...
<h1 {...fromCMS}>
like image 153
Chuck Avatar answered Feb 12 '26 11:02

Chuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!