简体   繁体   English

未定义调度no-undef

[英]dispatch is not defined no-undef

i am new to redux and i'm trying to dispatch an action from inside a function but it says "dispatch is not defined no-undef". 我是redux的新手,我正在尝试从函数内部调度操作,但它说“调度未定义为no-undef”。 Can anyone tell me what i'm doing wrong 谁能告诉我我在做什么错

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);

i have to make an api call with the year selected from saga but i don't know how to do it. 我必须从saga中选择年份进行api调用,但我不知道该怎么做。

here's my action 这是我的行动

import { createAction } from 'redux-actions';

export const REQUEST_YEAR = 'REQUEST_YEAR';
export const requestYear = createAction(REQUEST_YEAR);

Please help me Thank you 请帮我谢谢

dispatch is a prop just like your other props, you need to call it with this.props.dispatch . 就像其他道具一样, dispatch是一个道具,您需要使用this.props.dispatch来调用它。

Also it's advisable to only pass in the actions that you want to call as props: 另外,建议仅传递您要调用的动作作为道具:

handleSelection(e) {
  this.props.requestYear( e.target.value );
}

...

export default connect( mapStateToProps, { requestYear })(SigninForm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM