Search code examples
javascriptreactjsreduxreact-reduxextreact

Why initial state from reducer is not passed to connected component?


So, I am trying to parse a store from ExtReact into a component from Reducer (following these instructions: http://docs.sencha.com/extreact/6.5.0/guides/extreact_stores_in_flux.html#extreact_stores_in_flux_-_option_1__in_the_redux_store). But it seems, I cannot pass a state (i.e. I am passing undefined state for store and data) from reducer, even though when I consol.log the store and the data object in reducer I get concrete values. To mention, reducer is added into index.js combineReducer function so it is read and that is not a problem.

So these, are the most important parts of the code:

  1. Reducer:
export function usershortcuts(state = initialState, action){
    switch(action.type){
        case types.USER_SHORTCUT_FETCH_DATA_SUCCESS:{
            console.log("User shortcut store = state.userShortCutStore); // I am getting a concrete result
            state.userShortCutData = state.userShortCutStore.getData();
            console.log("User shortcut data = " + state.userShortCutData); //I am getting a concrete result
            return{...state};
        }
        default:{
            console.log(state.userShortCutStore);
            return {...state};
        }
    }
}
  1. Initial state:
export default {
    /* User shortcuts store. */
    userShortCutStore: userShortcutStore,

    userShortCutData: []
}
  1. Index.js:
const store = configureStore();

store.dispatch(usershortcutFetchDataThroughStore());

launch(
  <Provider store = {store}>
      <HashRouter>
        <Switch>

         {/* <Route path="/" name="Home" component={TextEditor}/>*/}
          <Route path="/" name="Home" component={Full}/>
        </Switch>
      </HashRouter>
  </Provider>
);
  1. ShortcutComponent which is inside Header, which is inside
class ShortcutComponent extends Component{

    constructor(props){
        super(props);
        this.state={
            userShortCutStore: null,
            userShortCutData: []
        }
    }

    componentDidMount(){
        this.setState({
            userShortCutStore: this.props.userShortCutStore,
            userShortCutData: this.props.userShortCutData
        })
    }

    render(){
        const userShortCutStore = this.state.userShortCutStore;
        const userShortCutData = this.state.userShortCutData;
        console.log("Store = " + userShortCutStore); //undefined
        console.log("User shortcut data = " + userShortCutData); //undefined
        return(
         /* .... */
        )
    }
}


const mapStateToProps = (state) => {
    return { 
        userShortCutStore: state.userShortCutStore,
        userShortCutData: state.userShortCutData
    }
};

export default connect(mapStateToProps) (ShortcutComponent);

Solution

  • const mapStateToProps = (state) => {
        return { 
            userShortCutStore: state.usershortcuts.userShortCutStore,
            userShortCutData: state.usershortcuts.userShortCutData
        }
    };