Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access a reducer of same logic into other logic. #156

Closed
arunfancraze opened this issue Aug 7, 2023 · 3 comments
Closed

How to access a reducer of same logic into other logic. #156

arunfancraze opened this issue Aug 7, 2023 · 3 comments

Comments

@arunfancraze
Copy link

Let us say I have following code

reducers(({ props }) => ({
        filters: [
            props.propertyFilters ? parseProperties(props.propertyFilters) : ([] as AnyPropertyFilter[]),
            {
                setFilter: (state, { index, property }) => {
                    const newFilters: AnyPropertyFilter[] = [...state]
                    newFilters[index] = property
                    return newFilters
                },
            },
        ],
        filtersOperatorsCache: [
            props.propertyFilters
                ? parsePropertiesForFiltersCache(props.propertyFilters)
                : ([] as FilterOperatorCache[]),
            {
                setFilterCaches: (state, { index, operator, property }) => {
                    const newFiltersCache: FilterOperatorCache[] = [...state]
                    newFiltersCache[index] = { ...newFiltersCache[index], [operator]: property }
                    return newFiltersCache
                },
            },
        ],
    })),

I want to access filtersOperatorsCache inside setFilter of filters reducer. How to do that.

@arunfancraze
Copy link
Author

arunfancraze commented Aug 7, 2023

I tried using reducers(({ values, props }) => ({ and then

setFilter: (state, { index, property }) => {
                    const newFilters: AnyPropertyFilter[] = [...state]
                    const cachedProperty = values.filtersOperatorsCache?.[index][property.operator]
                    newFilters[index] = cachedProperty ? cachedProperty : property
                    return newFilters
                },

but I am getting an error

Uncaught Error: You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store.
    at Object.getState (redux.js:196:13)
    at Object.a [as getState] (<anonymous>:1:38452)
    at getStoreState2 (index.cjs.js:1013:29)
    at Object.get [as filtersOperatorsCache] (index.cjs.js:1744:37)
    at Object.setFilter [as set filter (lib.components.PropertyFilters.propertyFilterLogic.0-$pageview-trends_TRENDS_data_exploration-filter)] (propertyFilterLogic.ts:54:40)

@arunfancraze arunfancraze reopened this Aug 7, 2023
@mariusandra
Copy link
Member

mariusandra commented Aug 7, 2023

You can't access other values inside a reducer. You can either pass in these values as part of an action, or use a listener for more complex logic flow.

@arunfancraze
Copy link
Author

Got it @mariusandra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants