在不渲染的轨道上做出反应

时间:2020-01-12 08:44:49

标签: ruby-on-rails reactjs

我正在尝试将我的react组件呈现在我的Rails应用程序的红宝石上。没有错误,但页面不显示任何内容。我怀疑该教程是否已过时或是否未能要求我导入一些东西。

我密切关注了这个站点:https://medium.com/quick-code/simple-rails-crud-app-with-react-frontend-using-react-rails-gem-b708b89a9419

这可能是本教程中最关键的部分。

Let’s return to config/routes.rb for a second and add our Home index page to be the root:

root to: 'home#index'

Now all our index page need is a corresponding view. Create a new ‘home’ folder inside app/views and add index.html.erb file to it:

app/views/home/index.html.erb

This file will only contain one line that takes advantage of react_component view helper that comes with ‘react-rails’ gem. It’s not going to work right now, but it all will make sense in a moment.

<%= react_component 'Main' %>

Creating our first React Component
Create a file named app/assets/javascripts/components/_main.js.jsx
We are going to follow the usual syntax for creating a stateless/presentational React Component that we will call Main:

const Main = (props) => {
    return(
      <div>
        <h1>Fruits are great!</h1>
      </div>
    )
}
Head to localhost:3000 in the browser and see for yourself that our first Component was loaded on the page. Pretty cool!

我试图包含类似的代码

import React from "react"
import ReactDOM from 'react-dom';

,但也不会呈现任何内容。 尝试调试时,我尝试不偏离本教程,因为我仍然是新手,并希望一次向一个来源学习,以防止发生冲突和使用不同的方法。

0 个答案:

没有答案