Select
我刚刚处理了这个问题,我发现将它抽象成自己的组件很有帮助。为此,您可以制作一个ReactReduxSelect.jsx
如下所示的组件 ( ):
import React from 'react';
import Select from 'react-select';
import style from 'react-select/dist/react-select.css';
// define portfolioSelector somehow
// or you could pass it as a property:
// portfolioSelector: React.PropTypes.func
class ReactReduxSelect extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<Select
name={this.props.name}
value={this.state.selected}
options={this.props.options}
onChange={(value) => {
this.setState({ selected: value });
portfolioSelector(value);
}}
placeholder={this.props.placeholder}
/>
);
}
}
ReactReduxSelect.propTypes = {
name: React.PropTypes.string,
placeholder: React.PropTypes.string,
options: React.PropTypes.array
};
export default ReactReduxSelect;
以这种方式实现它的酷之处在于,它可以在原生 Redux 状态树中更新。因此,如果您返回要嵌入此控件(MyTotallySickReactReduxWebpage.jsx
或其他任何内容)的页面,则可以导入组件...
import ReactReduxSelect from './ReactReduxSelect';
然后将其嵌入您的代码中...
const ArtifactGenerator = ({
// my totally awesome props
}) => (
// some react redux stuff w/ divs and { and js and all that jazz
<ReactReduxSelect
name="portf"
options={[
{ label: 'Market', value: 'All' },
{ prodType: { label: 'ProdType', value: 'All' }
]}
placeholder="Select Portfolio"
/>
// whatever other groovy code you want in there
);
// the rest of your props and export code or whatever for the page
我不完全确定你在initialValues
那里试图做什么,语法对我来说看起来不正确,所以我只是写了一些我认为更有可能工作的东西,但你可以很容易地调整它以适应您的需求。希望这可以帮助!