React-router - 检查空状态和重定向?

时间:2017-08-08 14:40:04

标签: reactjs react-router

如果需要,我如何检查空状态并重定向到第一条路线?

我有我的routes.js

export default (
    <Route path="/" component={App}>
        <IndexRoute component={Screen1} />
        <Route path="/1" component={Screen1} />;
        <Route path="/2" component={Screen2} />
        <Route path="/3" component={Screen3} />
        <Route path="/4" component={Screen4} />
        <Route path="/5" component={Screen5} />
        <Route path="/6" component={Screen6} />
    </Route>
);

和我的index.js

const store = configureStore();
console.log({ store });
render(
    <Provider store={store}>
        <Router history={browserHistory} routes={routes} />
    </Provider>,
    document.getElementById('root')
);

如何使用react-router

执行此操作

app.js

import React, { PropTypes } from 'react';
import Header from './common/Header';
import ModalRoot from './modals/Modal';
import Footer from './common/Footer';
class App extends React.Component {
    render() {
        return (
            <div className="container-fluid">
                <ModalRoot />
                <Header />
                {this.props.children}
                <Footer />
            </div>
        );
    }
}

App.propTypes = {
    children: PropTypes.object.isRequired,
};

export default App;

1 个答案:

答案 0 :(得分:1)

以下是使用{{3} }@4.0

的代码段
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';

const Wildcard = () => {
  <Redirect to="/" />
};

const store = configureStore();
console.log({ store });
render(
    <Provider store={store}>
        <Switch>
          <Route exact path="/" component={YOURCOMPONENT} />
          <Route exact path="/:section/:news/:title" component={App} />
          <Route exact path="/:section/:sector/:news/:title" component={App} />
          <Route exact path="/*" component={Wildcard} />
        </Switch>
    </Provider>,
    document.getElementById('root')
)

在第一个Route中插入代表“index”的组件名称。更新您的文件index.js

我在YOURCOMPONENT路径下面插入了另外两条路线,只是为了让这个例子看起来更真实,但它是可选的。