简体   繁体   English

对象无法获取方法或属性“ getCars”

[英]object can't get method or property 'getCars'

i'm working on react-redux intermidiate..but i don't know what's going wrong on this project 我正在对react-redux进行中介..但是我不知道这个项目出了什么问题

hera i have creacted the searchbar for getting car details..and the file is created as 'search.js'...you can see here.. 赫拉,我已经创建了搜索栏以获取汽车详细信息..并且文件被创建为“ search.js” ...您可以在这里看到。

search.js search.js

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import { getCars } from '../actions';
    import { bindActionCreators } from 'redux';

    class Search extends Component{
        constructor(props){
          super(props);

          this.state = {
            keyword:''
          }
        }

      searchCars = (event) => {
        event.preventDefault();
        this.props.getCars(this.state.keyword)
      }

      handleChange = (event) => {
          this.setState({
            keyword:event.target.value
          })
      }

      componentDidMount(){
        console.log(this.state);
      }

      render(){
        return(
          <div className="main_search">
            <form onSubmit={this.searchCars}>
                <input type="text" value={this.state.keyword} onChange = {this.handleChange} />
            </form>
          </div>
        )
      }
    }

    // mapStateToProps
    // mapDispatchToProps

    function mapDispatchToProps(dispatch){
      return bindActionCreators({getCars}, dispatch)
    }

    export default connect(null,mapDispatchToProps)(Search);

and i think error comes from here about getCars ..which is described below as s 'index.js'...you can see here 而且我认为错误来自关于getCars ..的错误,下面将其描述为s'index.js'...您可以在这里看到

index.js index.js

    const URL_ROOT = 'http://localhost:3004'

    export default function getCars(keywords){
        const request = fetch(`${URL_ROOT}/carsIndex?q=${keywords}`,
          {method:'GET'})
        .then(response => response.json())

        return{
          type:'SEARCH_CARS',
          payload:request
        }
    }

and the error looks like this.. 错误看起来像这样。 错误

and error showing in bundle.js file 和错误显示在bundle.js文件中 在此处输入图片说明

so try to fix it and help me... 所以尝试修复它并帮助我...

Please change your mapDispatchToProps method as 请更改您的mapDispatchToProps方法为

    const mapDispatchToProps = (dispatch)=> (
    bindActionCreators(getCars, dispatch) 
)

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

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