I'm trying to implement react-router in my project and experiencing huge concept misunderstanding . For some of components in my app I need to fetch data from server. Previously I did it with this code:
$.get('/getSmData', options, function (result) {
// set state with result or whatever
}.bind(this));
placed in componentDidMount or in onClick functions but now, in order to make server routing, all requests go to my routing file.
How do I fetch data?
This is routing part of my server:
var renderToString = require( 'react-dom/server').renderToString
var match = require('react-router').match
var RouterContext = require('react-router').RouterContext
var routes = require('./routes')
app.get('*', function (req, res) {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
res.render('index', {
react: renderToString(React.createElement(RouterContext, renderProps)),
})
} else {
res.status(404).send('Not found')
}
})
})
This is routes.js
module.exports= (
<Router history={browserHistory}>
<Route path='/' component={index.App}>
<Route path='/firstPage' component={First}>
<Route path=':firstPageChild' component={FirstCh}/>
</Route>
</Route>
<Route path='/secondPage' component={Second}/>
</Route>
</Router>
);
And let's say I need to fetch some data in child of one of children of firstPageChild in onButtonClick function. I can't prerender data and send with markup, so how should I do that?