我试图创建一个搜索功能来更新商店中的状态,但是一旦我停止搜索,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())
}