There are three ways to import image...
1) If you are direct using jsx.erb
or js.erb
file...
var image_path = "<%= asset_path(my_image.png) %>"
OR
render: function() {
return (
<img src={"<%= asset_url('path/to/image.png') %>"} />
)
}
2) Passing as props Through .erb
file to .js.erb
or .jsx.erb
file
in your .erb
file
render_component('Component', img_src: image_url('the_path'))
in your .js.erb
or .jsx.erb
file
render: function() {
return (
<img src={this.props.img_src} />
)
}
3) Recommended: Use .js
and .jsx
file and render it using view file .html.erb
In view file example.html.erb
<%= react_component("Example", props: {}, prerender: false) %>
In .jsx
file Example.jsx
import React, { Component } from "react";
import Image from "<IMAGE_PATH>/image.png";
export default class Example extends Component {
constructor(props, _railsContext) {
super(props);
}
render(){
return(
<div>
<img src={Image} alt="example" />
</div>
)
}
}
You need to register Example
component. In your app > javascript > packs > application.js
import ReactOnRails from "react-on-rails";
import Exmaple from "<COMPONENT_PATH>/Exmaple";
ReactOnRails.register({
Exmaple
});
Source: github