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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With