te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - onEnter Transitions with React Router and Redux Simple Router Dont Render New Route's Component - Stack Ove
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onEnter Transitions with React Router and Redux Simple Router Dont Render New Route's Component - Stack Ove

programmeradmin4浏览0评论

I have an app using react @0.14, redux @3.05, react-router @1.0.3, and redux-simple-router @2.0.2. I'm trying to configure onEnter transitions for some of my routes based on store state. The transition hooks successfully fire and push new state to my store, which changes the url. However, the actual ponent that is rendered on the page is the original ponent handler from the route match, not the new ponent handler for the new url.

Here is what my routes.js file looks like

export default function configRoutes(store) {
  const authTransition = function authTransition(location, replaceWith) {
    const state = store.getState()
    const user = state.user

    if (!user.isAuthenticated) {
      store.dispatch(routeActions.push('/login'))
    }
  }

  return (
    <Route ponent={App}>
      <Route path="/" ponent={Home}/>
      <Route path="/login" ponent={Login}/>
      <Route path="/dashboard" ponent={Dashboard} onEnter={authTransition}/>
      <Route path="/workouts" ponent={Workout} onEnter={authTransition}>
        <IndexRoute ponent={WorkoutsView}/>
        <Route path="/workouts/create" ponent={WorkoutCreate}/>
      </Route>
    </Route>
  )
}

Here is my Root.js ponent that gets inserted into the DOM

export default class Root extends React.Component {
  render() {
    const { store, history } = this.props
    const routes = configRoutes(store)

    return (
      <Provider store={store}>
        <div>
          {isDev ? <DevTools /> : null}
          <Router history={history} children={routes} />
        </div>
      </Provider>
    )
  }
}

To clarify, if I go to '/workouts', it will fire the onEnter authTransition hook, dispatch the redux-simple-router push action, change the url to '/login', but will display the Workout ponent on the page. Looking in Redux DevTools shows that state -> router -> location -> pathname is '/login'.

The state flow is

  1. @@INIT
  2. @@ROUTER/UPDATE_LOCATION (/workouts)
  3. @@ROUTER/UPDATE_LOCATION (/login)

Am I passing the store to the routes incorrectly? I can't figure out why the next Router/Update_Location doesn't work

I have an app using react @0.14, redux @3.05, react-router @1.0.3, and redux-simple-router @2.0.2. I'm trying to configure onEnter transitions for some of my routes based on store state. The transition hooks successfully fire and push new state to my store, which changes the url. However, the actual ponent that is rendered on the page is the original ponent handler from the route match, not the new ponent handler for the new url.

Here is what my routes.js file looks like

export default function configRoutes(store) {
  const authTransition = function authTransition(location, replaceWith) {
    const state = store.getState()
    const user = state.user

    if (!user.isAuthenticated) {
      store.dispatch(routeActions.push('/login'))
    }
  }

  return (
    <Route ponent={App}>
      <Route path="/" ponent={Home}/>
      <Route path="/login" ponent={Login}/>
      <Route path="/dashboard" ponent={Dashboard} onEnter={authTransition}/>
      <Route path="/workouts" ponent={Workout} onEnter={authTransition}>
        <IndexRoute ponent={WorkoutsView}/>
        <Route path="/workouts/create" ponent={WorkoutCreate}/>
      </Route>
    </Route>
  )
}

Here is my Root.js ponent that gets inserted into the DOM

export default class Root extends React.Component {
  render() {
    const { store, history } = this.props
    const routes = configRoutes(store)

    return (
      <Provider store={store}>
        <div>
          {isDev ? <DevTools /> : null}
          <Router history={history} children={routes} />
        </div>
      </Provider>
    )
  }
}

To clarify, if I go to '/workouts', it will fire the onEnter authTransition hook, dispatch the redux-simple-router push action, change the url to '/login', but will display the Workout ponent on the page. Looking in Redux DevTools shows that state -> router -> location -> pathname is '/login'.

The state flow is

  1. @@INIT
  2. @@ROUTER/UPDATE_LOCATION (/workouts)
  3. @@ROUTER/UPDATE_LOCATION (/login)

Am I passing the store to the routes incorrectly? I can't figure out why the next Router/Update_Location doesn't work

Share Improve this question edited Mar 9, 2016 at 23:59 Jeremy Banks - Vive le Canada 130k88 gold badges358 silver badges381 bronze badges asked Jan 21, 2016 at 16:52 JonJon 6,2654 gold badges23 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Turns out you want to use the react-router api (replace), not the redux-simple-router to control your transitions.

const authTransition = function authTransition(nextState, replace, callback) {
  const state = store.getState()
  const user = state.user

  // todo: in react-router 2.0, you can pass a single object to replace :)
  if (!user.isAuthenticated) {
    replace({ nextPathname: nextState.location.pathname }, '/login', nextState.location.query)
  }

  callback()
}

Also, be careful. I saw a lot of documentation out there for the react-router replace where you pass a single object. That's for react-router 2.0-rc*. If you're using react-router 1.0, you're going to want to pass replace 3 separate arguments.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论