我正在尝试在我的 React 应用程序中使用 redux 实现搜索功能。但不知怎的,出了点问题,我找不到问题所在。// Header.jsimport React from 'react';import { useDispatch } from 'react-redux';import { SEARCH_PIZZA } from '@ActionTypes';...other imports// styles for Headerconst useStyles = makeStyles(...)function Header() { const classes = useStyles(); const dispatch = useDispatch(); const [searchValue, setSearchValue] = React.useState(''); const handleSearch = (e) => { setSearchValue(e.target.value); dispatch({ type: SEARCH_PIZZA, searchValue }); }; return ( <div className={classes.root}> <AppBar position="static"> <Toolbar> <div className={classes.search}> <div className={classes.searchIcon}> <SearchIcon /> </div> <InputBase placeholder="Search…" inputProps={{ 'aria-label': 'search' }} name="pizzaSearch" value={searchValue} onChange={handleSearch} classes={{ root: classes.inputRoot, input: classes.inputInput, }} /> </div> </Toolbar> </AppBar> </div> );}export default Header;// reducer.jsimport { SEARCH_PIZZA } from '@ActionTypes';const initialState = { ...other state values searchValue: '',};function shopReducer(state = initialState, action) { switch (action.type) { case SEARCH_PIZZA: const { searchValue } = action; const pizzaList = state.pizzaList.filter((item) => item.name.match(searchValue, 'gmi'), ); return { ...state, searchValue, pizzaList }; ...other code }}export default shopReducer;我正在使用react-redux库中的钩子(如useReducer和useSelector)我正在考虑将搜索逻辑移至组件中,但我希望它保留在 redux 的上下文中。结果,搜索只返回一个空数组......值得一提的是我有一个持久状态设置
添加回答
举报
0/150
提交
取消