5

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.

1 Answer 1

10

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks It worked, now I need to fix my searchbar to filter on my TODO list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.