自定义组件未与 redux 连接
CustomizedComponent not connected with redux
我有 2 个自定义组件。
我把它们放在一个 Field 中,这样我就可以保存它们的价值。
CustomizedComponent2 与 redux 完美连接,我可以取它的值,但 CustomizedComponent1 不行。为什么?
<div className="mainMenu">
<form name="handleProductsForm">
<label>Product</label>
<Field
component={CustomizedComponent1}
name="products"
items={[
{ value: "bag", id: 1 },
{ value: "purse", id: 2 }
]}
/>
<Field component={CustomizedComponent2} name="category" type="text"/>
</div>
let ProductsPanel= reduxForm({
form: "handleProductsForm",
enableReinitialize: true
})(ProductsPanelWrapped);
const mapStateToProps = (state, ownProps) => {
return {
initialValues: {
}
};
};
import {connect} from 'react-redux';
import { yourActionName} from '../../actions/yourActionName';
onSubmit(){
const {dispatch} = this.props
dispatch(yourActionName(this.state.items))
}
// getting data from comp1 to Field
<div className="mainMenu">
<form name="handleProductsForm">
<label>Product</label>
<Field
component={CustomizedComponent1}
name="products"
getItems={(items)=> this.myItems(items)}
/>
<Field component={CustomizedComponent2} name="category" type="text"/>
</div>
myItems(items){
this.setState(items: items)
}
//component 1
let {getItems} = this.props
//call this function when you selected all values
getItems(this.state.items)
我有 2 个自定义组件。 我把它们放在一个 Field 中,这样我就可以保存它们的价值。 CustomizedComponent2 与 redux 完美连接,我可以取它的值,但 CustomizedComponent1 不行。为什么?
<div className="mainMenu">
<form name="handleProductsForm">
<label>Product</label>
<Field
component={CustomizedComponent1}
name="products"
items={[
{ value: "bag", id: 1 },
{ value: "purse", id: 2 }
]}
/>
<Field component={CustomizedComponent2} name="category" type="text"/>
</div>
let ProductsPanel= reduxForm({
form: "handleProductsForm",
enableReinitialize: true
})(ProductsPanelWrapped);
const mapStateToProps = (state, ownProps) => {
return {
initialValues: {
}
};
};
import {connect} from 'react-redux';
import { yourActionName} from '../../actions/yourActionName';
onSubmit(){
const {dispatch} = this.props
dispatch(yourActionName(this.state.items))
}
// getting data from comp1 to Field
<div className="mainMenu">
<form name="handleProductsForm">
<label>Product</label>
<Field
component={CustomizedComponent1}
name="products"
getItems={(items)=> this.myItems(items)}
/>
<Field component={CustomizedComponent2} name="category" type="text"/>
</div>
myItems(items){
this.setState(items: items)
}
//component 1
let {getItems} = this.props
//call this function when you selected all values
getItems(this.state.items)