3

Perhaps this is very specific question, but i believe it would be find to find solution. I find a lot of people ask about environment change after login/logout.

I'm using redux to store app state logic about login, modals, forms etc. and relay for data fetching. After login/logout I'm using connected router to trigger rerender with new environment

const RelayApp = connect(_ => ({
  routes: routes,
  environment: environment,
  history: browserHistory,
  render: applyRouterMiddleware(useRelay)
}))(Router);

ReactDOM.render(
  <Provider store={store}>
    <RelayApp />
  </Provider>,
  document.getElementById('root')
);

I've got my routes defined like this:

export default (renderAppRoute) => (
  <Route render={renderAppRoute} path="/" component={App} queries={AppQueries} >
    <IndexRoute component={UserList} queries={UserListQueries}/>
    <Route path=":username" component={User} queries={UserQueries} />
  </Route>
);

renderAppRoute function is defining what to render in different relay states, like if is fetching done

const renderAppRoute = ({ done, props, element }) => {
  if(props) { //it's loaded
    React.cloneElement(element, props);
  }
  return <h1>Loading...</h1>;
};

After i change env I'm getting error (index):7 Uncaught TypeError: Cannot read property 'user' of undefined(…). It's because on initial fetch, props is undefined and there's fetching in progress. After fetching is done in props you can find fragment definitions of RelayContainer etc. But after switch to new environment, props is never undefined, so it is rendering my component but without data in store. Is it possible to somehow detect or is there some way to identify if environment is new or do we need to create some custom logic on user side?

I'm currently making it with new variable isNewEnvironment which I set to true, when i create new environment, and false after renderAppRoute has params.done set to true

jonathancardoso
  • 9,319
  • 6
  • 49
  • 63
M.Svrcek
  • 5,050
  • 3
  • 21
  • 33
  • When defining `RelayApp`, is `environment` set to `Relay.Store`? If so, you might be able to check something in `Relay.Store._storeData`. I ran into an issue with webpack hot reload trying to do the network layer injection more than once, so I check to see if `Relay.Store._storeData._networkLayer._implementation` is defined. This isn't documented to my knowledge, but something in `_storeData` might be able to help you. – Alex Dec 12 '16 at 14:31

0 Answers0