【发布时间】:2015-01-03 18:13:45
【问题描述】:
我想做这样的事情
var App = React.createClass({
render: function() {
return (
<CountryAutoComplete />
)
}
});
不同的应用
var App2 = React.createClass({
render: function() {
return (
<CountryAutoComplete />
)
}
});
这是一个简单的自动完成(不是整个文件)
var AutoComplete = React.createClass({
componentDidMount: function() {
$(this.getDOMNode()).typeahead(this.props);
},
render: function() {
return (
<input type="text" class="typeahead" onChange={this.props.onChange} />
);
}
});
CountryAutoComplete 将是这样的自包含。
var CountryAutoComplete = React.createClass({
search: function(country, process) {
// Make an ajax call and return the data. No other components needed
$.ajax({
url: '/country' + '?search=' + country
}).then(process);
},
render: function() {
return (
<AutoComplete onChange={this.props.onChange} source={this.search} />
);
}
});
根据 Flux 文档,看起来任何带有 API 调用的东西都需要通过
actions -> API -> Dispatcher -> stores -> 组件
这使得 CountryAutoComplete 绑定到特定应用,因为操作、调度程序和商店是特定于应用的。使此组件可跨应用重用的最佳方法是什么?
【问题讨论】:
标签: reactjs reactjs-flux