Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Redux TypeError: dispatch is not a function

I'm trying to create a Search bar to my React TODO App.

Here's my Searchbar class:

import React, { Component } from 'react';
import { FilterTasks } from '../../redux/actions/searchbar';
import searchreducer from '../../redux/reducers/searchbar';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

class SearchBar extends Component{
    constructor(props){
        super(props);
        this.state = {term: ''};
    }

    render(){
        return(
            <div>
            <input 
                className='searchbar' 
                placeholder="Filter by task" 
                onChange={term => this.props.FilterTasks(term.target.value)} />
            </div>
        );
    }
}

function MapDispatchToProps(dispatch){
    return bindActionCreators({FilterTasks},dispatch); 
}

export default connect (MapDispatchToProps)(SearchBar);

Here's my action:

export const FilterTasks = (filter) => {
    return {
        type: 'FILTER_TASKS',
        filter
    }
}

And here's my reducer:

const SearchReducer = (state = {},action) =>{
    switch(action.type){
        case 'FILTER_TASKS':
        return {
            tasks: state.tasks.filter(it => it.task.toLowerCase().includes(action.filter.toLowerCase()))
        };
        break;
    }
    return state;
}

When I write anything on the searchbar I automatically get the error:

 Uncaught TypeError: dispatch is not a function
    at Object.FilterTasks

Any ideas on this?, I'm not sure if my search bar is well implemented like this.

like image 419
pedrodotnet Avatar asked May 02 '18 18:05

pedrodotnet


1 Answers

As a first argument connect expects to get a mapStateToProps function. So you need to explicitly pass an undefined instead of it.

export default connect(undefined, MapDispatchToProps)(SearchBar);
like image 96
Aliaksandr Sushkevich Avatar answered Sep 22 '22 22:09

Aliaksandr Sushkevich