簡體   English   中英

如何在只有 react-rails gem 的 rails 應用程序中使用供應商 React 模塊/組件?

[英]How to use vendor React modules / components within a rails application with only react-rails gem?

使用評論,我嘗試在我的 rails 應用程序中使用 Draft.js。

但我不斷收到錯誤:

Draft.min.self-b1d4414….js?body=1:1 Uncaught ReferenceError: Immutable is not defined

誰能告訴我,我犯的可能明顯的錯誤是什么?

詳情:

我只使用 react-rails 來保持簡單。 Gemfile 有以下相關行:

gem 'react-rails'
gem 'bootstrap-sass'

我將Draft.min.js放在app/assets/javascripts/externallibraries/ ,將Draft.css放在app/assets/stylesheets/externallibraries

相關文件application.js

/*
 //= require jquery
 //= require jquery_ujs
 //= require turbolinks
 //= require react
 //= require react_ujs
 //= require bootstrap-sprockets
 //= require react
 //= require react_ujs
 //= require externallibraries/Draft.min
 //= require components
 //= require_tree .
*/

application.css

/*
 *= require externallibraries/Draft
 *= require_tree .
 *= require_self
*/

最后我的 jsx 文件是:

class NewPost extends React.Component {
    constructor() {
        super();
        this.state = {
            editorState: EditorState.createEmpty()
        };
        this.onChange = (editorState) => this.setState({editorState});
    }

    componentDidMount(){
    }

    componentWillMount() {
    };

    componentWillUnmount() {
    }

    componentWillReceiveProps(nextProps) {
        if(JSON.stringify(this.props.some_field) !== JSON.stringify(nextProps.some_field) && nextProps.retrieved_data != null) // Check if it's a new user, you can also use some unique, like the ID
        {
            this.setState({some_field: nextProps.some_field});
        }
    } 

    render() {
        return (
            <div>
                {"My state is: " + JSON.stringify(this.state, null, "   ")}
                {"And my props are: " + JSON.stringify(this.props, null, "  ")}
                <Editor editorState={this.state.editorState} onChange={this.onChange} />
            </div>
        );
    }
}

這是我找到的解決方案。 也許可以挽救某人的一天:

  1. 在 rails 項目的根目錄中安裝包。 而不是使用紗線似乎更好。 命令是yarn add draft-js

  2. config/application.rb添加以下行:

    config.assets.paths << Rails.root.join('node_modules', 'draft-js', 'dist')

  3. app/assets/javascriptsapplication.js添加以下內容:

    //= 需要草稿

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM