I'm using Redux and React-router to make a simple Mail app. Since I'm rather new to Redux, I'm not quite understand actual data flow in Redux + Router.
What I'm trying to get
- After page starts (
/), MailListComponent fetches array of messages from server. At this time MessageComponent is not displayed, as it has no single message to fetch data for it. - After
state.messages:[]is fetched, app is navigated to the first message ofstate.messages:[](/messages/1)`. - After transition is finished, MessageComponent is shown and fetches message with id=1 info and it's in a separate request it's attachments.
Here's the component model:
What I'm doing
// MailListActions.js
export function loadMessages() {
return {
type: 'LOAD_MESSAGES',
promise: client => client.get('/messages')
};
}
// MailListReducer.js
import Immutable from 'immutable';
const defaultState = { messages: [], fetchingMessages: false };
export default function mailListReducer(state = defaultState, action = {}) {
switch (action.type) {
case 'LOAD_MESSAGES_REQUEST':
return state.merge({fetchingMessages: true});
case 'LOAD_MESSAGES':
return state.merge({fetchingMessages: false, messages: action.res.data || null});
case 'LOAD_MESSAGES_FAILURE':
// also do something
default:
return state;
}
}
As I'm using promiseMiddleware, LOAD_MESSAGES, LOAD_MESSAGES_REQUEST and LOAD_MESSAGES_FAILURE are dispacted as request /messages ends.
And now:
- Is it OK to dispatch
loadMessages()in componentDidMount of MailListComponent? - How should it be transitioned to
/messages/1properly? - Should I create
activeMessageId<Integer>in my state? - How all these components should be connected with React-Router?
Here's my current tries:
export default (store) => {
const loadAuth = (nextState, replaceState, next) => { ... };
return (
<Route name="app" component={App} path="/" onEnter={loadAuth}>
<IndexRoute component={Content}/> // <== THIS IS A DUMMY COMPONENT. It diplays pre-loader until the app is transitioned to real first message
<Route path="messages/:id" component={Message}/>
</Route>
);
};
Could you provide me some points, how to connect the dots? What is poper async data flow logic?
I'm using isomorphic-redux example as base for my app. Though is isomorphic, it shouldn't be too big difference between normal Redux app
Thank you.
UPDATE
One of the ideas — to set onEnter hook for <IndexRoute component={Content}/>, that will fetch messages, set into state and initialte transition. Is it redux+router way?
However, this way also may be rather tricky, 'cause /messages only works for authenticated users (where store.getState().auth.get('loaded') == true)

