I have a route with params and I want to render/redirect to a 404 route if an action returns no data from an external API. What would be the most sensible way to do this?
This is the router:
export default (
<Route path="/" ponent={App}>
<Route path=":venueName" ponent={Venue} />
<Route path="*" ponent={NotFound} />
</Route>
);
The Venue ponent is connected to Redux and fires a action:
// ...
ponentWillMount() {
this.props.actions.fetchVenue(this.props.id);
// want to redirect to 404 if this returns success === false
}
// ...
Action looks like this:
export const fetchVenue = (id) => ({
type: 'FETCH_VENUE',
payload: axios.get('http://someAddress/api/venues/id'),
});
The problem I have here is how do I figure out to do this with server-side rendering and for any amount of routes. I'm using "match" from react-router in express to render the markup. Please me know if you want me to post more code.
I have a route with params and I want to render/redirect to a 404 route if an action returns no data from an external API. What would be the most sensible way to do this?
This is the router:
export default (
<Route path="/" ponent={App}>
<Route path=":venueName" ponent={Venue} />
<Route path="*" ponent={NotFound} />
</Route>
);
The Venue ponent is connected to Redux and fires a action:
// ...
ponentWillMount() {
this.props.actions.fetchVenue(this.props.id);
// want to redirect to 404 if this returns success === false
}
// ...
Action looks like this:
export const fetchVenue = (id) => ({
type: 'FETCH_VENUE',
payload: axios.get('http://someAddress/api/venues/id'),
});
The problem I have here is how do I figure out to do this with server-side rendering and for any amount of routes. I'm using "match" from react-router in express to render the markup. Please me know if you want me to post more code.
Share Improve this question edited Oct 12, 2016 at 17:00 Vlady Veselinov asked Oct 12, 2016 at 16:54 Vlady VeselinovVlady Veselinov 5,4315 gold badges28 silver badges50 bronze badges1 Answer
Reset to default 8The ponent isn't the right place for this kind of logic, IMHO. I do this in my action, and use react-router
's `browserHistory. So something like:
export function fetchBrand(id) {
return function(dispatch) {
fetchData(`brands/${id}`, {
headers: { 'X-Token-Auth': localStorage.getItem('token')}
}).then(response => {
dispatch({
type: FETCH_BRAND_SUCCESS,
payload: response
});
}).catch(err => {
if (err.response.status == 404) {
browserHistory.push("/my404page");
}
});
}
}
And I'd have some sort of route
to my404page
, for example.