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 - React.js pass parameters with Router not in URL - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React.js pass parameters with Router not in URL - Stack Overflow

programmeradmin3浏览0评论

Hi I want to move from one page to another and pass paramters search and type. Can I achieve this with react router without these parameters in the URL ?

I am looking at this .md#dynamic-segments and the solutions using <RouteHandler {...this.props}/> but it is not working until I pass params to the url.

Is there any solution for that ?

EDIT 1. Routes:

<Route name="app" path="/" handler={App}>
    <Route name="realestates" handler={RealEstatesPage}/>
    <Route name="realestatesPrefiltered" path="realestatesPrefiltered/:search/:type" handler=RealEstatesPage}/>
    <Route name="realestate" handler={RealEstateDetails}/>
    <DefaultRoute handler={MainPage}/>
</Route>

Hi I want to move from one page to another and pass paramters search and type. Can I achieve this with react router without these parameters in the URL ?

I am looking at this https://github./rackt/react-router/blob/master/docs/guides/overview.md#dynamic-segments and the solutions using <RouteHandler {...this.props}/> but it is not working until I pass params to the url.

Is there any solution for that ?

EDIT 1. Routes:

<Route name="app" path="/" handler={App}>
    <Route name="realestates" handler={RealEstatesPage}/>
    <Route name="realestatesPrefiltered" path="realestatesPrefiltered/:search/:type" handler=RealEstatesPage}/>
    <Route name="realestate" handler={RealEstateDetails}/>
    <DefaultRoute handler={MainPage}/>
</Route>
Share Improve this question edited Apr 24, 2015 at 17:26 Cory Danielson 14.5k4 gold badges46 silver badges52 bronze badges asked Jan 11, 2015 at 20:23 Piotr LeniartekPiotr Leniartek 1,1872 gold badges15 silver badges33 bronze badges 5
  • How and where are you defining your routes (e.g. <Routes>)? Please share your link to a jsfiddle or jsbin. – rxgx Commented Jan 12, 2015 at 0:18
  • Should it only be passed to certain routes? If not, perhaps you could store those values in some sort of application state? – Markus-ipse Commented Jan 13, 2015 at 7:46
  • @rxgx I edited post with routes – Piotr Leniartek Commented Jan 13, 2015 at 21:15
  • @Hummlas yes hummlas I only want to pass it to realestatesPrefiltered route. – Piotr Leniartek Commented Jan 13, 2015 at 21:18
  • possible duplicate of react-router - pass props to handler ponent – Jim G. Commented Sep 23, 2015 at 16:30
Add a ment  | 

3 Answers 3

Reset to default 5

The link you mentioned outlines the two different strategies. First, dynamic segments are just parameterized URLs, where parameters are sent as part of the URL path instead of in the query string. So these would need to be publicly visible.

Second, you can use props. This is the only supported option for passing things down "behind the scenes." In addition, it's the preferred method in React. The question is, where can this happen?

One option is to pass the values in at the top-level, from within Router.run(). This is outlined in the link above, with each nested handler passing the props down, either explicitly, like <RouteHandler search={this.props.search} />, or wholesale using spread attributes like <RouteHandler {...props} />.

Since you only want it to happen for a subset of pages, you could use nested routes, wherein the parent route handler owns the properties in question as part of its state. It would then just pass them to every child as normal props in the same way as the above case.

For example, your route map might look like this:

<Route name="app" path="/" handler={App}>
    <Route name="realestates" handler={RealEstatesPage}/>
    <Route name="realestatesPrefiltered" handler={RealEstatePrefiltered}>
       <Route name="homes" handler={RealEstateHouseDetails}/>
       <Route name="apartments" handler={RealEstateHouseDetails}/>
       <Route name="land" handler={RealEstateLandDetails}/>
    </Route>
    <Route name="realestate" handler={RealEstateDetails}/>
    <DefaultRoute handler={MainPage}/>
</Route>

Then your RealEstatePrefiltered ponent might look like this:

RealEstatePrefiltered = React.createClass({
    getInitialState: function () {
        return { search: '', type: '' }; // Update from store or event.
    },
    render: function() {
        return <div>
            <RouteHandler search={this.state.search} type={this.state.type} />
        </div>;
    }
});

The only question left is, how do you change the state on this ponent? You could use a Flux store for this, or you could just set up a global event and have this ponent watch for changes.

you can use state in history, like this

history.push({
  pathname: '/about',
  search: '?the=search',
  state: { some: 'state' }
})

In your first ponent, you can trigger this onCick of a link or button

browserHistory.push({
    pathname: `/about`,
    search : 'search'
    state: {
        type: 'type'
    }
});

In your other ponent, you can now grab them like:

constructor(props) {
    super(props);
    this.state = {
        search: props.location.search,
        type: props.location.state.type
    }
}
发布评论

评论列表(0)

  1. 暂无评论