ped*_*net 5 javascript reactjs redux react-redux
我正在尝试为我的 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);
Run Code Online (Sandbox Code Playgroud)
这是我的操作:
export const FilterTasks = (filter) => {
return {
type: 'FILTER_TASKS',
filter
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的减速机:
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;
}
Run Code Online (Sandbox Code Playgroud)
当我在搜索栏上写任何东西时,我会自动收到错误消息:
Uncaught TypeError: dispatch is not a function
at Object.FilterTasks
Run Code Online (Sandbox Code Playgroud)
对此有任何想法吗?我不确定我的搜索栏是否像这样实现得很好。
Ali*_*ich 10
作为第一个参数connect期望得到一个mapStateToProps函数。所以你需要明确地传递一个undefined而不是它。
export default connect(undefined, MapDispatchToProps)(SearchBar);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8176 次 |
| 最近记录: |