[英]Working with the same ReactJS form component on the same page
我对reactJS相当陌生,我想知道在单个页面上处理相同表单组件的最佳方法是什么。 请记住,我正在使用助焊剂,并且该组件正在与商店交谈。
例如:<SearchForm /> <SearchForm />
当我尝试使用表单#1输入字段时,表单#2同时从表单#1获取值。 我认为问题出在商店。 这些组件正在同一个商店讲话,而该商店正在一次更新所有组件。
我该如何处理这个问题?
这是我到目前为止的代码。
const SearchField = React.createClass({
propTypes: {
isSearchActivated: React.PropTypes.bool.isRequired,
},
_onChange() {
var previousHighlightedIndex = this.state.highlightedIndex;
this.setState(getStateFromStores(), function() {
if (previousHighlightedIndex == 0 &&
this.state.highlightedIndex == -1) {
this.refs.SearchBar.selectAll();
}
});
},
componentDidMount() {
if (window.location.pathname == "/" && !Modernizr.mq("screen only and (max-width: 768px)")) {
$(ReactDOM.findDOMNode(this.refs.SearchBar)).find("input").focus();
}
},
componentWillUnmount() {
SearchResultStore.removeChangeListener(this._onChange);
},
onChangeSearchString(e) {
SearchResultsUtils.search(e.target.value);
},
onBlur(e) {
var self = this;
var cb = function() {
if (!self.state.selectedResult && self.state.results.length) {
self.handleSelectedResult(0);
}
SearchResultsActions.disallowResultsDisplay();
};
if($(".search-bar").hasClass("active")) {
$(".search-bar.active").removeClass("active");
}
},
onFocus(e) {
$(ReactDOM.findDOMNode(this.refs.SearchBar)).closest(".search-bar").addClass("active");
},
handleSubmit() {
var self = this;
},
render() {
var className = "search-bar clearfix";
return (
<div className={className}>
<div className="search-bar-search">
<SearchBar
searchString={this.state.searchString}
onChange={this.onChangeSearchString}
onKeyDown={this.onKeyDownSearchString}
onFocus={this.onFocus}
onBlur={this.onBlur}
placeholder="Search Meds or Conditions"
ref="SearchBar" />
</div>
<SearchButton
handleSubmit={this.handleSubmit} />
</div>
);
},
});
module.exports = SearchField;
我在这里先向您的帮助表示感谢。
绝对可以在整个应用程序中多次重用组件。
1.如果您不想有任何状态:
在您的情况下,您可以提供表单提交处理程序作为回调道具。 如果您不想在搜索表单上保持任何状态。 例如对于表格1
<Searchform submitHandler={searchForm1Handler}/>
对于表格2
<Searchform submitHandler={searchForm2Handler}/>
和在您的搜索表单组件内部
render(){
return (
<form id="searchform" onSubmit={this.props.submitHandler} role="form">
// other input and buttons
</form>
)
}
2.有状态
通过这种方法,每个组件将具有自己的单独状态,这些状态对它们是私有的。
这是示例组件来说明这一点
import React ,{ Component } from 'react';
export default class SearchForm extends Component {
constructor(props){
super(props);
this.state = {
searchTerm : ''
};
this.submit = this.submit.bind(this);
this.changeSearchTerm = this.changeSearchTerm.bind(this);
}
submit(e){
e.preventDefault();
let searchTerm = this.state.searchTerm;
//Now perform some action based on search term you get
}
changeSearchTerm(e){
this.setState({searchTerm :e.target.value});
}
render(){
return(
<div>
<div className="row">
<div className="col-md-12">
<form role="form" className="form-horizontal" onSubmit={this.submit}>
<fieldset>
<div className="form-group">
<div className="col-sm-6">
<input id="st" type="text" placeholder="search term" onChange={this.changeSearchTerm} value={this.state.searchTerm} required autofocus/>
</div>
</div>
<div className="form-group">
<div className="col-xs-12 text-center">
<button className="btn">
Search
</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
)
}
}
现在使用它们
对于表格1
<Searchform/>
对于表格2
<Searchform/>
React具有状态组件:
组件可以维护内部状态数据(通过this.state访问)
这使您可以将<SearchForm />#1
和value1
以及<SearchForm />#2
和value2
存储在此this.state
要构建表单,请检查以下内容: https : //facebook.github.io/react/docs/forms.html
看起来您的商店只为一种表单保存一种状态-包括搜索值。
如果您两次渲染此表单,则两个表单在每次更改时都将检索相同的状态。 因此,根据设计,它们是彼此的精确副本。 如果一个表单发生更改,则两个表单都会注意到更改,并且两个表单都会从存储中检索完全相同的状态。
要解决此问题,我建议:
<SearchForm formID='form1'/> <SearchForm formID='form2'/>
formID
向下传递给formID
所有子项(例如<SearchField>
<SearchField>
,确保您的组件仅通过其自己的formID
呈现状态。 您可能需要更新getStateFromStores()
方法本身。 或者在<SearchField>
内部<SearchField>
过滤掉相关的内容,然后再将其传递给setState()
。 SearchResultsUtils.search(e.target.value);
所做的事情),那么您还需要传递formID
。 因此,商店将知道要更新的表格。 PS:我认为您的代码在componentDidMount()
缺少一些行:您有删除监听器以存储更改的代码,但是缺少用于添加监听器的代码。 您的组件中缺少用于调用_onChange()
方法的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.