redux-async-connect icon indicating copy to clipboard operation
redux-async-connect copied to clipboard

Cannot read property 'loaded' of undefined

Open bdefore opened this issue 10 years ago • 7 comments

After adding this to UR (https://github.com/bdefore/universal-redux/pull/52), upon loading the page without any asyncConnect, I get the error: Uncaught TypeError: Cannot read property 'loaded' of undefined. The error isn't source mapped to anything and there's no reference to loaded anywhere in the universal-redux-starter that I'm using to verify things are working correctly.

bdefore avatar Jan 19 '16 17:01 bdefore

@bdefore I checked and could not find any references to loaded property in redux-async-connect... https://github.com/Rezonans/redux-async-connect/blob/master/modules/asyncConnect.js here is the only setting this property....

If you give me your source code where you're checking this, i can try to help My skype is sarssokol.

sars avatar Jan 20 '16 00:01 sars

I see the problem. I was following along with the README, but there it used createStore from a ./redux/create that was not shown. I needed to add it to my reducers along with my redux-simple-router, like so:

import { combineReducers } from 'redux';
import { routeReducer } from 'redux-simple-router';
import { reducer as reduxAsyncConnect } from 'redux-async-connect';

export default combineReducers({
  reduxAsyncConnect,
  routing: routeReducer
});

bdefore avatar Jan 20 '16 04:01 bdefore

Yes, thanks @bdefore

fixed: https://github.com/Rezonans/redux-async-connect/commit/973e465dea1670db1b560d456d77fda7634d21c5

sars avatar Jan 20 '16 10:01 sars

@sars I think it may be best to check first if reduxAsyncConnect exists before acting on it. The use case I'm thinking of is for example with UR, where I can set up the necessary hooks to make it work universally, but a user may or may not want to use the functionality. As it stands they'd need to include it in their reducers even if they didn't use it, and I'd need to include it as a peer dependency, or else they'd see this error.

bdefore avatar Jan 20 '16 15:01 bdefore

@bdefore when you're building universal app , you don't wand to reload data on client than was just loaded on server. loaded key allows us to understand that data was loaded... so, we cannot remove this dependency

sars avatar Jan 20 '16 16:01 sars

@sars i'm not proposing removing the loaded check, but rather to add a check that reduxAsyncConnect is among the reducer modules that a project has defined. if they have forgotten or don't want it, it shouldn't cause a script error.

bdefore avatar Jan 20 '16 17:01 bdefore

@bdefore May be it's better to add some warning when it seems, that user did not connect reducer? If we just add smth like:

const state = this.context.store.getState().reduxAsyncConnect;
const dataLoaded = state.loaded;
if (!dataLoaded) { // we dont need it if we already made it on server-side
    this.loadAsyncData(this.props);
}

loadAsyncData will be invoked all the time if there was no reducer connected when you are using it in universal app - behaviour will be different: it will load data on server and then load it again on client.

sars avatar Jan 20 '16 23:01 sars