最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to redirect to 404 if no data from external API (universal React + Redux app)? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

The 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.

发布评论

评论列表(0)

  1. 暂无评论