全部源代碼、文檔和圖片都在 github 的倉庫裏,點擊進入倉庫javascript
// /src/containers/NotFound/index.js import React, { Component } from 'react'; class NotFound extends Component { componentWillMount() { const { staticContext } = this.props; staticContext && (staticContext.NotFound = true); } render() { return ( <div>404 NotFound</div> ); } } export default NotFound;
export default [ { path: '/', component: App, key: 'app', routes: [ { path: '/', component: Home, loadData: Home.loadData, exact: true, key: '/' }, { path: '/news', component: News, exact: true, key: '/news' }, { component: NotFound } ] } ];
if (context.NotFound) { res.status(404); } res.send(html);
import React, { Component } from 'react'; import {Redirect } from 'react-router-dom'; class News extends Component { render() { return ( <Redirect to="/" /> ); } } export default News;
{ csses: [], action: 'REPLACE', location: { pathname: '/', search: '', hash: '', state: undefined }, url: '/' }
if (context.action === 'REPLACE') { res.redirect(301, context.url); } res.send(html);
if (context.action === 'REPLACE') { res.redirect(301, context.url); } else if (context.notFound) { res.statusCode = 404; } res.send(html);