javascript - rails/ react : Refs Must Have Owner

标签 javascript ruby-on-rails reactjs

我正在使用react-rails gem。在我的组件中,我有两个输入字段,每个字段都有一个引用。但是,控制台向我抛出以下错误:

Uncaught Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded

Facebook 的文档没有帮助。

_new_item.js.jsx

var NewItem = React.createClass({
    handleClick() {
        var name = this.refs.name.value;
        var description = this.refs.description.value;

       console.log('The name value is ' + name + 'the description value is ' + description);

    },

    render() {
        return (
            <div>
                <input ref='name' placeholder='Enter the name of the item' />
                <input ref='description' placeholder='Enter a description' />
                <button onClick={this.handleClick}>Submit</button>
            </div>
        );
    } 
});

_body.js.jsx

var Body = React.createClass({
    render() {
        return (
            <div>
                <NewItem />
                <AllItems />
            </div>
        );
    } 
});

_main.js.jsx

var Main = React.createClass({
    render() {
        return (
            <div>
                <Header />
                <Body />
            </div>
        );
    }
});

我真的不知道错误在哪里。

最佳答案

您最好使用函数 refs 而不是命名的 refs:

所以而不是

<input ref='name' />

您可以使用:

<input ref={node => this.nameElement = node} />

然后代替:

this.refs.name

使用

this.nameElement

我相信它会解决您的问题。

关于javascript - rails/ react : Refs Must Have Owner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45109020/

相关文章:

javascript - 使用react datepicker,需要为今天的日期和选择的日期应用不同的样式

javascript - 在 ReactJS 中下载带有文本的图片

javascript - NodeJS 和 MongoDB 单元测试

ruby-on-rails - 组织多个项目的 git 存储库的好方法

javascript - Google Earth Engine 中带有 map() 函数的双循环

jquery - 如何正确地将模型与 2 个不同的对象关联?(Rails 4)

ruby-on-rails - 使用 nginx 运行 capybara

css - reactjs中的分页格式

php - 如何将 javascript var 的innerHTML 设置为 php/html 文本

javascript - 将包含在对象内部的参数传递给函数是个坏主意吗?