gpt4 book ai didi

reactjs - React.js 输出到 jsc : ReferenceError: is not defined

转载 作者:行者123 更新时间:2023-12-03 13:22:07 29 4
gpt4 key购买 nike

我正在 Rails 项目中使用 react-rails gem。虽然 javascriptjsx 一致工作,但我的 coffeescript 文件似乎从未工作。请注意,我尝试使用纯 coffeescript 解决方案,没有插值 jsx。

在我的 coffeescript 文件中(其扩展名为 *.js.coffee )

{div} = React.DOM

Hello = React.createClass
render: ->
(div {}, ['Hello ' + @props.name])

在我看来:

= react_component 'Hello', name: 'World'

这是我在控制台中不断遇到的错误:

ReferenceError: Hello is not defined

最佳答案

取 self 的 GitHub issuereact-rails 存储库中,jakubmal 回答:

CoffeeScript creates a closure, which will likely look like:

(function() {
var div, hello;

div = React.DOM.div;

Hello = React.createClass({
render: function() {
return div({}, ['Hello ' + this.props.name]);
}
});
}).call(this);

making Hello inaccessible outside the closure.

What you could do is to assign Hello to window like:

window.Hello = React.createClass

or using a shortcut/trick:

@Hello = React.createClass

To keep your app structure clean, you will need to apply at least a namespace pattern here. http://addyosmani.com/blog/essential-js-namespacing/

此外,Paul O'Shannessy 在 react-rails 谷歌群组中写道:

The helper is pretty naive and expects your components to be available as globals. Coffeescript wraps each file in a closure before they get joined by sprockets which violates the global assumption. This came up during development but we decided something for some people would be better than nothing for anybody.

关于reactjs - React.js 输出到 jsc : ReferenceError: <component> is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22130629/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com