异步React select与redux
我试图用redux创建一个异步react-select组件,但不知何故无法在下拉列表中获得搜索结果。这是很新的。请帮忙:)异步React select与redux,redux,react-select,Redux,React Select,我试图用redux创建一个异步react-select组件,但不知何故无法在下拉列表中获得搜索结果。这是很新的。请帮忙:) import React,{PropTypes}来自'React'; 从“反应路由器”导入{Link}; 从'react redux'导入{connect}; 从“反应选择”导入选择; 从“../../actions/institutions”导入{fetchInstitutionsIfNeeded}; 类注册扩展了React.Component{ 建造师(道具){ 超级(
import React,{PropTypes}来自'React';
从“反应路由器”导入{Link};
从'react redux'导入{connect};
从“反应选择”导入选择;
从“../../actions/institutions”导入{fetchInstitutionsIfNeeded};
类注册扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
值:null
};
this.getInstitutions=this.getInstitutions.bind(this);
this.onChange=this.onChange.bind(this);
}
onChange(输入){
这是我的国家({
值:输入
});
}
政府机构(投入){
const{dispatch}=this.props;
如果(!输入){
返回Promise.resolve({options:[]});
}
分派(获取机构如果需要(输入));
}
render(){
让选项=this.props.options;
返回(
);
}
}
常量mapStateToProps=(状态)=>({
选项:state.institutions.options
});
导出默认连接(MapStateTops)(注册);
另外,选项对象的格式也正确,并且在redux存储中得到了正确的更新,但在select async的下拉列表中没有反映出来。试试看,我们也可以从操作中返回,但它打破了还原器的整个概念
//actions.js
导出常量getProducts=(输入=“”)=>async(分派)=>{
派遣({
类型:GET_PRODUCTS_PENDING,
有效载荷:{},
});
试一试{
const response=wait axios.get(`/api/v1/products/`);
派遣({
类型:获得产品成功,
有效载荷:response.data,
});
}捕获(错误){
//处理错误
}
};
//components.jsx
类注册扩展了PureComponent{
异步getProductsForSelect(输入){
等待这个.props.getProducts(输入);
返回this.props.product.options;
}
render(){
const{handleSubmit}=this.props;
返回(
this.getProductsForSelect(e)}
/>
);
}
}
看看这个,应该很有用。您需要从redux存储状态读取数据。在上面的例子中,您只是发送了一个没有返回任何内容的操作。我使用redux与react select进行了大量工作,但是,我不太清楚您的问题是什么,您能解释一下您的问题吗?!谢谢,这正是我需要的。
import React, { PropTypes } from 'react';
import { Link } from 'react-router';
import { connect } from 'react-redux';
import Select from 'react-select';
import { fetchInstitutionsIfNeeded } from '../../actions/institutions';
class Signup extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null
};
this.getInstitutions = this.getInstitutions.bind(this);
this.onChange = this.onChange.bind(this);
}
onChange(input) {
this.setState({
value: input
});
}
getInstitutions(input) {
const { dispatch } = this.props;
if (!input) {
return Promise.resolve({ options: [] });
}
dispatch(fetchInstitutionsIfNeeded(input));
}
render() {
let options = this.props.options;
return (
<div>
<Select.Async
name="institute"
value={this.state.value}
autoload={false}
onChange={this.onChange}
loadOptions={this.getInstitutions}
/>
</div>
);
}
}
const mapStateToProps = (state) => ({
options: state.institutions.options
});
export default connect(mapStateToProps)(Signup);