Geoff
Geoff

Reputation: 309

What is the reason getState() not functioning in React-Redux?

export const SMSAdmin_Filter_User = (userName) => (dispatch, getState) => {
    var st = getState();
    ...
}

When this code runs, getState() is defined in the debugger as a function, but st comes up as undefined. I have used getState in multiple other action creator functions with great success, so am uncertain why it is not functioning here.

This function is called as a promise since there are other asynchronous processes running (incremental fetch for large number of users).

Here is where it is being called from:

componentDidMount() {
        var el = document.getElementById("userList");
        if (el) {
            el.focus({ preventScroll: true });
        }
        // console.log("SMSAdmin")
        new Promise((res,rej)=>this.props.SMSAdmin_Get_Users());
        // .then(() => {
        //     this.setState({ users: this.props.SMSAdmin.users });
        // });
    }

    filterUsers = () => {
        let target = document.getElementById("userName");
        let name = target?.value?.toLowerCase();
        new Promise((res, rej)=>SMSAdmin_Filter_User(name?.trim()));
    }

filterUsers() is also being called from the render function to ensure updates when SMSAdmin_Get_Users() adds more users to the list.

Here is the mapDispatchToProps():

const mapDispatchToProps = (dispatch) => {
    return {
        SMSAdmin_Get_Users: () => { return dispatch(SMSAdmin_Get_Users()) },
        SMSAdmin_Load_User: (userId, userName, oldData = null, startVal = 0, number = 20) => {
            return dispatch(SMSAdmin_Load_User(userId, userName, oldData, startVal, number))
        },
        SMSAdmin_Update_User: (user, province, credits) => { return dispatch(SMSAdmin_Update_User(user, province, credits)) },
        SMSAdmin_setCurrentUpload: (userName) => { return dispatch(SMSAdmin_setCurrentUpload(userName)) },
        SMSAdmin_Filter_User: (userName) => { return dispatch(SMSAdmin_Filter_User(userName)) },
    }
}

I am not able to provide a sandbox for the code because there are multiple other files associated with this component and the data being used is confidential.

Thanks.

Edit: Showing redux store creation

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { reducers } from './reducerMain.js';

export const ConfigureStore = () => {

    const store = createStore(
        reducers,
        applyMiddleware(thunk, logger)
    );

    return store;
}

Upvotes: 1

Views: 78

Answers (1)

Greg Motyl
Greg Motyl

Reputation: 2545

I guess you are accidently using imported function (not the one mapped in mapDispatchToProps). Did you forgot to use the one from props ? like that:

    filterUsers = () => {
        // ...
        new Promise((res, rej)=>this.props.SMSAdmin_Filter_User(name?.trim()));
    }

Upvotes: 1

Related Questions