HackerNoon.com
Published in

HackerNoon.com

Photo credit

How to get your Rails data into your React component with webpacker

$ rails new myapp --webpack=react
$ ./bin/rails webpacker:install$ ./bin/rails webpacker:install:react
react_component(‘Appointments’, appointments: @appointments)
<%= content_tag :div,
id: "appointments_data",
data: @appointments.to_json do %>
<% end %>
#appointments_data{data: @appointments.to_json}
document.addEventListener('DOMContentLoaded', () => {
const node = document.getElementById('appointments_data')
const data = JSON.parse(node.getAttribute('data'))
ReactDOM.render(
<Appointments appointments={data} />,
document.body.appendChild(document.createElement('div')),
)
})

Sign up for Get Better Tech Emails via HackerNoon.com

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Share your ideas with millions of readers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store