javascript - React.js 插入另一个组件的 ref

标签 javascript reactjs components refs

我开始学习react.js,遇到了找不到答案的情况。

这是我的组件结构(这只是为了显示层次结构):

-- ItemContainer
----- ItemList
-------- ItemSingle
----- ItemForm

ItemList 和 ItemForm 是兄弟,ItemContainer 的 child 。 ItemSingle 是 ItemList 的子项。

在每个 ItemSingle 上我都有一个“编辑”按钮,它应该用它的内容更新表单,但我无法从 ItemSingle 发送到 ItemForm.refs。

这是我试过的

ItemSingle 组件:

var ItemSingle = React.createClass({
    insertEdit: function(edit_item, e){
        e.preventDefault();
        React.findDOMNode(ItemForm.refs.title).value = edit_item.title;
        React.findDOMNode(ItemForm.refs.content).value = edit_item.content;
    },
    render: function() {
        return (
            <div className="box">
                <div className="box-body bigger-box">
                    <h4>
                        <strong>#{this.props.index + 1}</strong>
                        <span className="title">{this.props.title}</span>
                        <span className="float-right box-option">
                            <a href="#" onClick={this.insertEdit.bind(this, this.props)}>Edit</a>
                        </span>
                    </h4>
                    <div className="text" dangerouslySetInnerHTML={{__html: this.props.content}} />
                </div>
            </div>
        );
    }
});

ItemForm 组件:

var ItemForm = React.createClass({
    handleSubmit: function(e) {
        e.preventDefault();
        var title = React.findDOMNode(this.refs.title).value.trim();
        var content = React.findDOMNode(this.refs.content).value.trim();

        if(!title || !content){ return false;   }

        this.props.onItemsAdd({title: title, content: content});
        React.findDOMNode(this.refs.title).value = '';
        React.findDOMNode(this.refs.content).value = '';
        $('textarea').trumbowyg('empty');
        return true;
    },
    componentDidMount: function() {
        $('textarea').trumbowyg();
    },
    render: function() {
        return (
            <div className="form-container">
                <form className="form" method="POST">
                    <div className="form-group">
                        <input type="text" className="form-control" ref="title"/>
                    </div>
                    <div className="form-group">
                        <textarea ref="content"></textarea>
                    </div>
                    <div className="form-group">
                        <a className="btn btn-success btn-flat btn-block btn-lg" onClick={this.handleSubmit}><i className="fa fa-save"></i>&nbsp;&nbsp;&nbsp;Save</a>
                    </div>
                </form>
            </div>
        );
    }
});

这是我在 ItemSingle 第 4 行遇到的错误:

Uncaught TypeError: Cannot read property 'title' of undefined

最佳答案

React 中的引用不应该像这样使用。

If you have not programmed several apps with React, your first inclination is usually going to be to try to use refs to "make things happen" in your app. If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. Often, it becomes clear that the proper place to "own" that state is at a higher level in the hierarchy. Placing the state there often eliminates any desire to use refs to "make things happen" – instead, the data flow will usually accomplish your goal.

在您的应用中,您将需要 state,可能在 ItemList 组件中,更改此状态的方法应传递给 ItemSingle 组件.

https://facebook.github.io/react/docs/more-about-refs.html

关于javascript - React.js 插入另一个组件的 ref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32614800/

相关文章:

javascript - Redux 如何保证没有竞争条件?

javascript - 无法通过 props 更新子状态

javascript - ReactJS:调用 setLoading 时状态没有改变

delphi - 如何在delphi中正确调用组件的自定义组件编辑器表单

Java编译错误: code too large

javascript - pg-promise,使用带有嵌套对象的命名参数

javascript - 异步循环 typescript 数组

reactjs - npm start 如何指定index.js以外的启动文件

javascript - 尝试使用 Math.random 和过滤器填充 16 个随机图像

javascript - NgFor 之后访问 ViewChildren 列表