简体   繁体   English

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

[英]React.js insert into ref of another component

I'm starting to learn react.js, and I've face a situation that I can't get an answer. 我开始学习react.js,并且遇到无法解决的情况。

This is my components structure(this is just to show hierarchy): 这是我的组件结构(这只是为了显示层次结构):

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

Where ItemList and ItemForm are siblings, chilren of ItemContainer. 其中ItemList和ItemForm是同级,是ItemContainer的孩子。 And ItemSingle is a child from ItemList. ItemSingle是ItemList的子级。

On each ItemSingle I have a "edit" button, that should update the form with its content, but I'm not able to send from ItemSingle to ItemForm.refs. 在每个ItemSingle上,我都有一个“编辑”按钮,该按钮应使用其内容更新表单,但是我无法从ItemSingle发送到ItemForm.refs。

This is what I've tried 这就是我尝试过的

ItemSingle Component: 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 Component: 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>
        );
    }
});

And this is the error I got on ItemSingle line number 4: 这是我在ItemSingle第4行上遇到的错误:

Uncaught TypeError: Cannot read property 'title' of undefined

Refs in React are not meant to be used like this. 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. 如果您尚未使用React编写多个应用程序,那么您的第一个倾向通常是尝试使用引用在应用程序中“使事情发生”。 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. 将状态放在那里通常消除了使用引用“使事情成真”的任何愿望–相反,数据流通常可以实现您的目标。

In your app you will need state , probably in ItemList component, and methods to change this state should be passed to the ItemSingle components. 在您的应用中,您将需要state ,可能在ItemList组件中,并且更改此状态的方法应传递给ItemSingle组件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM