I can't register components using React_on_Rails gem - ruby-on-rails

The registration was working fine with the default HelloWorld app, but once I deleted the folder and created my own, it stopped working.
I made a new folder under app/bundles called posts with a startup folder.
In the startup folder, I'm registering the components like so:
import ReactOnRails from 'react-on-rails';
import PostContainer from '../containers/PostContainer';
ReactOnRails.register({
PostContainer
});
The PostContainer.jsx file is in the containers folder and it looks like this:
import React, { PropTypes, Component } from 'react';
export default class PostsContainer extends React.Component {
render() {
return (
<div>
<Header />
<PostList posts={this.props.posts} />
</div>
)
}
}
My webpack.config.js file looks like this:
entry: [
'es5-shim/es5-shim',
'es5-shim/es5-sham',
'babel-polyfill',
'./app/bundles/HelloWorld/startup/registration',
],
I tried changing the HelloWorld to posts or Posts, but it did not work.
Am I supposed to have a file called webpack.configure.build.js? Or is the webpack.config.js the file I need to edit?
Any help would be appreciated!

Did you add the component to the erb view file with the associated props passed in? You would need to add the props within your Posts controller or whichever controller is rendering your Posts view. I'm pretty sure you would then need to add a line in your webpack config:
config: {
'webpack-bundle': [
'...',
'./app/bundles/Posts/startup/registration'
]
}
Then in your view:
<%= react_component("PostsContainer", props: #posts_props, prerender: false) %>
Also make sure in your layout file you have:
<%= javascript_pack_tag 'webpack-bundle' %>
Note: PropTypes should be imported from the prop-types package now. The react team plucked that package out of the react core as of recent.

Related

Import googlemaps via importmaps in Rails 7

What is the way to go to load GoogleMaps via importmaps in Rails7?
I've seen folks import it via the script-tag like this:
<script src="https://maps.googleapis.com/maps/api/js?key=<%= Rails.application.credentials[:p10_google_maps_js_api_key] %>&callback=initMap" async defer data-turbolinks-eval="false"></script>
but I would like to take advantage of the new importmap feature of Rails 7. Unfortunately I don't understand how i could trigger the initMap callback without using it as a script.
So there is a Stimulus component that you could use stimulus-places-autocomplete. However, you could easily implement this yourself and save you the trouble of pulling in a dependency.
At the end of the day, the google callback needs to be fired off. The way most have gotten around this is by creating an event and attaching that to the window. You then add a data-action to your views controller div that will look for this even and fire a callback of its own. That callback being an initializer within your Stimulus controller itself.
##########places_controller.rb##########
import { Controller } from "#hotwired/stimulus";
export default class extends Controller {
static targets = ["street", "city", "state", "zip"];
connect() {
// Can also initialize this with a CustomEvent
const event = new Event('google-maps-callback', {
bubbles: true,
cancelable: true,
})
window.dispatchEvent(event);
}
// additional actions
}
##########index.html.erb##########
<div
data-controller="places"
data-action="google-maps-callback#window->places#initMap"
>
<%# view code here %>
</div>
This still is not a solution that I love. However, its the workaround that we have for now.

Shopify Polaris Page tag not working - Rails and reactJS

I'm trying to make a shopify app and I want to use ruby on rails with polaris and reactJS. When I try to use the Page component taken from shopify's website, nothing ever gets rendered to the screen. Here's an example of what I'm talking about.
This is my index.jsx file:
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import {AppProvider, Page, Card} from '#shopify/polaris'
import '#shopify/polaris/styles.css'
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<Page title="Jar Witock-Lid"> //Here is where I use the Page tag
<p>Other content</p>
</Page>,
document.body.appendChild(document.createElement('div')),
)
})
Everything else is working fine because when I change the Page tag to be a div like this:
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<div>
<div>{"One"}</div>
<div>{"Two"}</div>
<div>{"Three"}</div>
</div>,
document.body.appendChild(document.createElement('div')),
)
})
It would render ("One" "Two" "Three") to the page. When I run my rails server and refresh the screen, no errors or warning are displayed. I downloaded polaris by running this command: yarn add #shopify/polaris.
Any help is greatly appreciated. I tried looking all over the web but I couldn't put the pieces together. Thanks in advance!
I found the solution. The only thing I was missing was wrapping it all in an AppProvider tag like this:
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<AppProvider>
<Page title="Jar Witock-Lid">
<p>content</p>
</Page>
</AppProvider>,
document.body.appendChild(document.createElement('div')),
)
})

react-rails asset pipeline image path

I'm using the react-rails gem, and I'm having trouble figuring out how to load images from the asset pipeline.
I handle this currently by using the .js.jsx.erb extension, but as far as I know this is unconventional. (am I correct?)
Sometimes I just use an empty div and and set the div's background-image property as the image I intend to use.
What is the best way to go about loading images to react components when using react-rails?
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

Angular 4 with haml-loader

I am trying to use haml with templates in my Rails 5.1 and Angular 4 project.
I have the haml template working but when I tried to implement Material 2 for Angular, it uses attributes that haml does not add to the DOM.
For example, I am trying to implement a sidenav and I created a component with the following template:
-#navbar.component.html.haml
%md-sidenav-container.example-container
%md-sidenav.example-sidenav{ '#sidenav' => true }
Jolly good!
.example-sidenav-content
%button{'md-button' => true, '(click)' => 'sidenav.open()'}
Button
And here is my component:
//navbar.component.ts
import { Component } from '#angular/core';
import navbar from './navbar.html.haml';
#Component({
selector: 'navbar',
template: navbar
})
export class NavbarComponent {}
'#sidvenav' attribute and 'md-button' attribute are not rendered to the DOM.
can I not use haml with Angular 4? should I not use haml with Angular 4? or am I just doing it wrong?
Under config/webpack/loaders I have a file called haml.js
module.exports = {
test: /\.html\.haml$/,
loader: 'haml-haml-loader'
}
And under javascript/app I have a file called haml.d.ts
declare module "*.haml" {
const content: string
export default content
}
This is what allows me to use template for haml. There are similar files for html and I render them as strings and they get compiled as needed by html-loader or haml-haml-loader.
Any help would be appreciated.

Rails React Component Renders But Not Visible on Page

I just added the react-rails gem to my project and tried to add a react component to my index.html.erb file. I used the react component generator to make a simple message/text element, but it does not show up on my view after I deploy to Heroku, but it seems to render when I inspect the page. I have no idea why this isn't working.
Please look at the images I have attached. It looks like the javascript doesn't get compiled??
Body inspection part 2
index.html.erb code
<h2>Dashboard</h2>
<%= react_component('Message', text: 'hello') %>
<h3>Products TEST REACT </h3>
Message.jsx code
var React = require("react")
class Message extends React.Component {
render () {
return (
<div>
<div>Text: {this.props.text}</div>
</div>
);
}
}
Message.propTypes = {
text: React.PropTypes.string
};
module.exports = Message
HTML body inspection
I reverted to the git commit before installing react-rails and used the shakacode react-on-rails gem instead and got it to work

Resources