'Redux Toolkit, useSelector returns the previous state

I'm new to react and react redux and im facing that problem where I can't get to the latest state provided by useSelector.

I'm trying to make a filter that allows users to filter products by categories using multiple checkboxes. (f.e. checking backpacks and jackets will show items with these characters included in the title)

I want to mention that when I used useRef hook for sorting function everything works fine and useSelector returns the current value.

This is the shop navigation where user can sort and filter items.

const ShopNavigation = () => {
    const filterValue = useSelector(state => state.filteringClothes.filterValue)
    const [filterCategory, setFilterCategory] = useState([])
    const typeRef = useRef('none')
    const dispatch = useDispatch()
    const sortHandler = () => {
        if (typeRef.current.value === 'none') {
            dispatch(sortingActions.sortNone())
        }
        if (typeRef.current.value === 'priceAsc') {
            dispatch(sortingActions.sortPriceAsc())
        }
        if (typeRef.current.value === 'priceDes') {
            dispatch(sortingActions.sortPriceDes())
        }
        if (typeRef.current.value === 'titleAsc') {
            dispatch(sortingActions.sortA_Z())
        }
        if (typeRef.current.value === 'titleDes') {
            dispatch(sortingActions.sortZ_A())
        }
    }
    const filterHandler = e => {
        if (e.target.checked === true) {
            setFilterCategory(filterCategory => [...filterCategory, e.target.value])
            dispatch(filteringActions.newFilterValue(filterCategory))
        } else {
            setFilterCategory(filterCategory.filter(item => item !== e.target.value))
            dispatch(filteringActions.newFilterValue(filterCategory))
        }
    }
    return (
        <div className='shop-navigation'>
            <div className='shop-navigation__wrapper'>
                <div className='shop-navigation__sort'>
                    <label htmlFor='sort'>Sort</label>
                    <select onChange={sortHandler} name='sort' id='sorting' defaultValue='none' ref={typeRef}>
                        <option value='none'>None</option>
                        <option value='priceAsc'>Lowest Price</option>
                        <option value='priceDes'>Highest Price</option>
                        <option value='titleAsc'>A-Z</option>
                        <option value='titleDes'>Z-A</option>
                    </select>
                </div>
                <div className='shop-navigation__category'>
                    <p>Categories</p>
                    <input type='checkbox' id='backpacks' value='backpack' onClick={filterHandler} />
                    <label htmlFor='backpacks'>Backpacks</label>
                    <input type='checkbox' id='jackets' value='jacket' onClick={filterHandler} />
                    <label htmlFor='jackets'>Jackets</label>
                    <input type='checkbox' id='t-shirts' value='shirt' onClick={filterHandler} />
                    <label htmlFor='t-shirts'>T-Shirts</label>
                    <input type='checkbox' id='sleeves' value='sleeve' onClick={filterHandler} />
                    <label htmlFor='sleeves'>Sleeves</label>
                    <input type='checkbox' id='casual' value='casual' onClick={filterHandler} />
                    <label htmlFor='casual'>Casual</label>
                </div>
            </div>
        </div>
    )
}

By pressing checkboxes, filterHandler is activated and dispatches an action of sending these values of strings to the filterValue state (array) in filtering slice.

import { createSlice } from '@reduxjs/toolkit'
const filteringSlice = createSlice({
    name: 'filtering',
    initialState: { filterValue: [] },
    reducers: {
        newFilterValue(state, action) {
            state.filterValue = action.payload
        },
    },
})

export const filteringActions = filteringSlice.actions
export default filteringSlice

In ProductList component. I need the latest data for the filterHandler (im currently working on it) from filtering-slice state to filter array of objects and show only these items that contain the characters which are stored in filterValue array.

Example: If filterValue's state is ['jacket', 'backpack'] I want to show objects that in their key title jacket or backpack word is included.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source