javascript - 在 reducer 中更新搜索功能的状态
问题描述
我正在尝试创建一个搜索功能来更新商店中的状态,但是一旦我停止搜索,reducer 实际上会向状态广告过滤结果。所以我最终重复了。
当搜索输入为空时,我分派初始提取,但过滤后的结果被添加到状态中。
这是我的减速机
switch(action.type){
case GET_ENTRIES:
return [ ...state, ...action.entries ]
case SEARCH_ENTRIES:
return [...action.entries]
default:
return state
}
我的行动
export const searchHandle = (query) => (dispatch, getState) => {
const theState = getState()
const entries = theState.entriesData.filter(val => (
val.firstName.includes(query) ||
val.lastName.includes(query) ||
val.phoneNumber.includes(query)
))
dispatch({
type:SEARCH_ENTRIES,
entries
})
}
和处理程序
const handleChange = (e) => {
e.target.value.length > 0 ? dispatch(searchHandle(e.target.value))
: dispatch(entriesCall())
}
解决方案
推荐阅读
- python - PermissionError: [Errno 13] - 我在尝试使用来自 Github 的“brownie”包时在我的 Ubuntu/WSL 设置中遇到了这个错误
- javascript - 在 svelte 中将值从子级传递给父级 - 绑定指令的直觉
- api - 我不想清除柏树中的现金和饼干
- python - 如何在python中生成独特的彩色图像
- flutter - 按下时浮动操作按钮的奇怪背景形状
- java - 3D对象SceneView Sceneform android的CameraPreview
- wordpress - wordpress 网站 url 上的前缀导致 404 错误
- javascript - 悬而未决的承诺已取消
- pcregrep - Pcregrep Perl 正则表达式积极的后视
- c - 接收缓冲区数据时套接字错误 10054 - winsock2.h 库