0

I am trying to implement ReactJS on a Ruby on Rails application using webpacker gem.

Does anyone know how to read/pass data attributes to ReactJS component.

Please find the code below.
in index.html.erb
<div id="data" data-item-id="<%= @item.id %>"></div>

in Data.jsx file

<!-- language: lang-js -->

class Data extends React.Component {
  constructor(props) {
    super(props);
  }

 componentDidMount() {
   this.getSiteDetails();
 }

 getSiteDetails(){
  fetch(i want to pass data attributes here along with the url)
 }

 render() {}
}

ReactDOM.render(<Data />, document.getElementById('data'));
Sandeep Garg
  • 1,292
  • 1
  • 13
  • 31
Charith H
  • 345
  • 1
  • 13

1 Answers1

1

You can do this:

 getSiteDetails(){
   const itemId = document.getElementById('data').dataset.itemId
   fetch(you can use itemId in here)
 }
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
Giang Le
  • 6,446
  • 4
  • 23
  • 26