我在这里粘贴了代码。问题是当使用连接mapstatetoprops和mapdispatchtoprops时,我得到了this.props.CarDetails不是一个函数..我做错了..我也想知道如何在点击按钮时编写actioncreator
import React,{Component} from 'react';
import {connect} from 'react-redux';
import {carDetails} from '../actions/index';
import {Increment} from '../actions/updateValueAaction';
import {bindActionCreators} from 'redux';
class SummaryItem extends Component{
componentWillMount(){
this.props.carDetails();
}
renderList(){
return this.props.cars.map((car) => {
return (
<div className="col-sm-6 incrementer" >
<div >
<span className="titleclass"> Hala </span>
<div className="input-group">
<span className="input-group-btn">
<button type="button" className="btn btn-default btn-number incdec" disabled="disabled" data-type="minus" value ={car.id} data-field="quant[1]">
<span className="glyphicon glyphicon-minus"></span>
</button>
</span>
<input type="text" name="quant[1]" value={car.ct} readOnly className="form-control input-number cthtvalue" />
</div>
</div>
</div>
</div>
);
});
}
render () {
return (
<div >{this.renderList()}</div>
);
}
onInputChange(e){console.log(e.target.value);
// thisObj.onInputChange(thisObj.car.ct, event.target.value);
}
}
function mapStateToProps(state){
return {cars: state.cars.all};
}
function mapDispatchToProps(dispatch){
return {
routerActions:bindActionCreators({carDetails},dispatch),
actions:bindActionCreators(Increment,dispatch)
};
}
export default connect(mapStateToProps,mapDispatchToProps)(SummaryItem);
答案 0 :(得分:2)
根据你在mapDispatchToProps中所写的内容,你应该像这样调用你的行动:
this.props.routerActions.carDetails()
答案 1 :(得分:1)
有一种更好的方法如何使用mapDispatchToProps函数。
function mapDispatchToProps(){
return {carDetails, Increment};
}
之后可以通过this.props.carDetails()
调用carDetails方法。