重定向willTransitionTo在react-router 2.4.0中?

时间:2016-05-02 05:58:06

标签: reactjs react-router

我目前在聊天应用程序中使用-- MySQL dump 10.13 Distrib 5.6.13, for Linux (x86_64) -- -- Host: localhost Database: test -- ------------------------------------------------------ -- Server version 5.6.13-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `articles` -- DROP TABLE IF EXISTS `articles`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `articles` ( `articlesID` int(5) NOT NULL DEFAULT '0', PRIMARY KEY (`articlesID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */;enter code here ,并且在没有用户时对如何重定向感到困惑。我不认为重定向可以在"react-router": "^2.4.0"中使用。

我应该在聊天路径上使用"^2.4.0"个问题?

这样的事情:

onEnter

routes.js

 <Route path="chat" component={Chat} onEnter={Chat.willTransitionTo}/>

Chat.jsx

 <Route path="/" component={App} >
    <IndexRoute component={Chat} />
    <Route path="chat" component={Chat} />
    <Route path="login" component={Login} />
 </Route>

2 个答案:

答案 0 :(得分:1)

至少存在拼写错误:

Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

应该是:

tansition.redirect('/login');

您还应该检查react-router:https://github.com/reactjs/react-router/tree/master/examples/auth-flow

的auth-flow示例

答案 1 :(得分:1)

我使用setRouteLeaveHook正常工作,如下所示: https://github.com/ReactTraining/react-router/blob/master/docs/guides/ConfirmingNavigation.md

  

返回false以防止转换而不提示用户

但是,他们忘记提及钩子也应该取消注册,请参阅herehere

我们可以使用它来实现我们自己的解决方案,如下所示:

class YourComponent extends Component {
  constructor() {
    super();

    const {route} = this.props;
    const {router} = this.context;

    this.unregisterLeaveHook = router.setRouteLeaveHook(
      route,
      this.routerWillLeave.bind(this)
    );
  }

  componentWillUnmount() {
    this.unregisterLeaveHook();
  }

  routerWillLeave() {
    // return false to prevent a transition w/o prompting the user,
    // or return a string to allow the user to decide:
    if (!this.state.isSaved) {
      return 'Your work is not saved! Are you sure you want to leave?'
    }
  }
  ...
}

YourComponent.contextTypes = {
  router: routerShape
};