ruby-on-rails React在Rails 7的视图中不呈现组件

gg58donl  于 2023-05-19  发布在  Ruby
关注(0)|答案(1)|浏览(156)

Visual Studio Code Picture
这是我目前的布局,如果你需要复制代码请从这里:

import * as React from 'react'
import  * as ReactDOM from 'react-dom'

const Welcome = () => {
    return(
        <div className="container">
            <h1>Hello World! Welcome to the Rails 7 Course with React JS</h1>
        </div>
    )
}

document.addEventListener('DOMContentLoaded', () => {
    ReactDOM.render(<Welcome />, document.getElementById('welcome'))
})

export default Welcome

问题是容器中的文本没有呈现在views/home/index.html.erb中,在那里您将看到<div id="Welcome"></div>
index.js有

import Welcome from './components/Welcome'

application.js有

// // Entry point for the build script in your package.json
// import "@hotwired/turbo-rails"
// import "./controllers"
import './react/src/index.js'
import * as bootstrap from "bootstrap"
Rails.application.routes.draw do
  # get 'home/index'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  root "home#index"
end
ffvjumwh

ffvjumwh1#

简单的错误,花了一些时间远离编码,当我回去弄清楚它相当快。

<div id="Welcome"></div> 

should be

<div id="welcome"></div>

相关问题