With react-router-redux, it appears as though the only way to get routing information is through props only. Is this right?
Here's roughly what I am doing in my app right now:
<Provider store={Store}>
<Router history={history}>
<Route path="/" component={App}>
<Route path="child/:id" />
</Route>
</Router>
</Provider>
App
const App = (props) =>
<div className="app">
<Header />
<Main {...props}/>
<Footer />
</div>
Main
const Main = (props) =>
<div>
<MessageList {...props}/>
</div>
MessageList
let MessageList = (props) => {
const {id} = props;
// now I can use the id from the route
}
const mapStateToProps = (state, props) => {
return {
id: props.params.id
};
};
MessageList = connect(mapStateToProps)(MessageList)
What I would like to do, is remove {...props} from all of my components, and turn MessageList into this:
let MessageList = (props) => {
const {id} = props;
// now I can use the id from the route
}
const mapStateToProps = (state) => {
return {
id: state.router.params.id
};
};
MessageList = connect(mapStateToProps)(MessageList)
Having to pass down props in everything feels like a big step back for how clean Redux made my application. So if passing params is correct, I'm wondering why thats preferable?
My specific case that brought this up:
I have an UserInput component that sends a message (dispatches a SEND_MESSAGE action). Depending on the current page (chat room, message feed, single message, etc) the reducer should put it in the correct spot. But, with react-redux-router, the reducer doesn't know about the route, so it can't know where to send the message.
In order to fix this I need to pass the props down, attach the id to my SEND_MESSAGE action, and now the otherwise simple UserInput is handling business logic for my application.
Container Component
which is not directly created byRouter
? So it is not possible to access the router params even if I usewithRouter
? If I can't create some nestedContainer Component
it is possible to send many props from theRouter
level deep into my nested component. – Nad