作为react-router路由组件中的回调的操作

时间:2016-06-14 15:06:08

标签: reactjs react-router react-router-redux

我想使用redux操作作为回应路由器路由中的<Route path="profile/search" component={ProfileSearch} onChange={store.dispatch(fetchCompanies())} /> 事件的回调。我也在使用react-router-redux。

这样的东西
fetchCompanies

其中Uncaught TypeError: hook.apply is not a function TransitionUtils.js?f913:22 是行动。

更一般地说,我希望听取路线变化并最终与州交互,这就是为什么使用动作对我来说似乎是最直接的选择。但是,我收到了这个错误:

<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.html">
            <img src="img/logo%20DM.png" alt="Logo De Morgen" />
          </a>
        </div>
        <div id="navbar" class="collapse navbar-collapse right">
          <ul class="nav navbar-nav navbar-right">
            <li>
              <a href="#">
                <img src="img/facebook.png" alt="Logo van facebook" />
              </a>
            </li>
            <li>
              <a href="#">
                <img src="img/twitter.png" alt="Logo van twitter" />
              </a>
            </li>
          </ul>
        </div>
        <!--/.nav-collapse -->
      </div>
    </nav>
    <section class="first-block">
      <div class="row">
        <div class="col-md-12">
          <h1>WIN DUO-COMBITICKETS VOOR            <br />
 ROCK WERCHTER & TW CLASSIC</h1>
        </div>
        <div class="col-md-12">
          <p class="subline">Zak dit jaar af naar de heilige weide van Werchter op kosten van De Morgen.            <br />
 Waag uw kans en maak kans op             <strong>duo-combitickets voor Rock Werchter</strong>
            <br />
 en             <strong>TW Classic</strong>
. </p>
          <img src="img/down.png" alt="Next button" />
        </div>
      </div>
    </section>
    <section class="second-block">
      <div class="row">
        <div class="col-md-6 image">
          <img src="img/rockwerchter.png" width="100%" height="auto" alt="" />
        </div>
        <div class="col-md-6 tekst">
          <h2>
                Rock Werchter
                            <br />

                30/06 - 03/07
                </h2>
          <p>
                    Rock Werchter is officieel én officeus nog steeds het beste festival ter wereld. Op de affiche staan toppers als Sir Paul McCartney, Ellie Goulding, Disclosure en New Order. Overnachten doet u in een luxueuze chalet met een royaal ontbijtbuffet, warmwaterdouches en buitenzwembad.
                </p>
        </div>
      </div>
    </section>
    <section class="second-block">
      <div class="row">
        <div class="col-md-6 col-md-push-6 image">
          <img src="img/rockwerchter.png" width="100%" height="auto" alt="" />
        </div>
        <div class="col-md-6 col-md-pull-6 tekst">
          <h2>
                Rock Werchter
                            <br />

                30/06 - 03/07
                </h2>
          <p>
                    Rock Werchter is officieel én officeus nog steeds het beste festival ter wereld. Op de affiche staan toppers als Sir Paul McCartney, Ellie Goulding, Disclosure en New Order. Overnachten doet u in een luxueuze chalet met een royaal ontbijtbuffet, warmwaterdouches en buitenzwembad.
                </p>
        </div>

      </div>
    </section>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')

    </script>
    <script src="js/bootstrap.js"></script>
  </body>

还有另一种方法可以达到这个目的吗?试图将一个动作作为回调传递给路线改变本身就不好吗?如果没有,我该如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

调用store.dispatch会引发错误,但在函数中包装已调度的操作会有效。

const onChangeProfileSearch = () => store.dispatch(fetchCompanies());
<Route path="profile/search" component={ProfileSearch} onChange={onChangeProfileSearch} />

但是,可能有更好的方法。