我是redux的新手,我正试图从一个函数内部调度一个动作,但是它说“调度没有定义为no-undef”。任何人都可以告诉我我做错了什么
import React, { Component } from 'react';
import {connect} from 'react-redux';
import {getYear} from '../actions/getYearAction';
import {requestYear} from '../actions';
class SigninForm extends Component {
constructor(props){
super(props);
this.state = {
car :{
year: '',
brand:[],
}
}
}
getYear(){
return this.props.year.map( (year, id) => {
return(
<option value={year} key={id}>{year}</option>
)
})
}
handleSelection(e){
dispatch(requestYear( e.target.value ));
}
render() {
return (
<div>
<form>
<select onChange = {this.handleSelection.bind(this)}>
<option value="" selected disabled hidden>--YEAR--</option>
{this.getYear()}
</select><br/>
<select>
<option value='' selected disabled>-Brand-</option>
</select>
<button type='submit'>Add</button>
{this.props.value}
</form>
</div>
);
}
}
function mapStateToProps(state){
return{
year: state.year
}
}
export default connect( mapStateToProps)(SigninForm);
我必须使用从saga中选择的年份进行api调用,但我不知道该怎么做。
这是我的行动
import { createAction } from 'redux-actions';
export const REQUEST_YEAR = 'REQUEST_YEAR';
export const requestYear = createAction(REQUEST_YEAR);
请帮帮我 谢谢
答案 0 :(得分:0)
dispatch
就像你的其他道具一样,你需要用this.props.dispatch
来称呼它。
另外,建议您只传递要作为道具调用的操作:
handleSelection(e) {
this.props.requestYear( e.target.value );
}
...
export default connect( mapStateToProps, { requestYear })(SigninForm);