我正在尝试为我的React TODO应用创建一个搜索栏。
这是我的Searchbar类:
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);
这是我的行动:
export const FilterTasks = (filter) => {
return {
type: 'FILTER_TASKS',
filter
}
}
这是我的减速机:
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;
}
当我在搜索栏上写任何内容时,我会自动收到错误:
Uncaught TypeError: dispatch is not a function
at Object.FilterTasks
关于这个的任何想法?,我不确定我的搜索栏是否像这样实现得好。
答案 0 :(得分:2)
作为第一个参数connect
期望获得mapStateToProps
函数。因此,您需要明确传递undefined
而不是它。
export default connect(undefined, MapDispatchToProps)(SearchBar);