gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:36:41 25 4
gpt4 key购买 nike

我开始学习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/

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