gpt4 book ai didi

javascript - React 路由器向下滚动新页面

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:50 26 4
gpt4 key购买 nike

我在使用 react-router ^2.4.1 时遇到了一个问题,如果我在我的主页上向下滚动然后转到一个新页面,它也会向下滚动,而不是在顶部(预期行为)。

我正在使用这个入门包:react-webpack-node我的 routes.jsx 看起来像这样

import React from 'react'
import { Route, IndexRoute } from 'react-router'
import cookie from 'react-cookie'

import App from 'containers/App'
import HomePage from 'containers/HomePage'
import WaitingListPage from 'containers/WaitingListPage'
import NotFoundPage from 'containers/NotFoundPage'
import SupportPage from 'containers/SupportPage'

/*
* @param {Redux Store}
* We require store as an argument here because we wish to get
* state from the store after it has been authenticated.
*/
export default (store) => {
const hasQueueToken = (nextState, replace, callback) => {
if (cookie.load('queueToken')) {
replace({
pathname: `/waiting-list/${cookie.load('queueToken')}`,
state: { nextPathname: nextState.location.pathname }
})
}
callback()
}

return (
<Route path='/' component={App}>
<IndexRoute component={HomePage} />
<Route path='/w_ref/:ref' component={HomePage} />
<Route path='/waiting-list/:token' component={WaitingListPage} />
<Route path='/waiting-list' onEnter={hasQueueToken} component={WaitingListPage} />
<Route path='/support' component={SupportPage} />
<Route path='/terms-and-conditions' component={TermsConditions} />
<Route path='/privacy-policy' component={PrivacyPolicy} />
<Route path='*' component={NotFoundPage} />
</Route>
)
}

最佳答案

React Router 从 2.0.0 版本开始不包括滚动状态管理。

推荐的方法是使用 react-router-scrollscroll-behavior 装饰路由器,如 in this example 所示:

import { applyRouterMiddleware, browserHistory, Router } from 'react-router';
import useScroll from 'react-router-scroll';

/* ... */

ReactDOM.render(
<Router
history={browserHistory}
routes={routes}
render={applyRouterMiddleware(useScroll())}
/>,
container
);

关于javascript - React 路由器向下滚动新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com