我正在使用react-router v4进行路由,解析查询参数,history.listen
这里推荐使用
我在生命周期钩子中调用它componentDidMount
以确保组件已安装,以便我可以使用这样的高阶组件将其作为组件状态提供:
import createHistory from 'history/createBrowserHistory';
import qs from 'qs';
import { compose, lifecycle, withState } from 'recompose';
export const history = createHistory();
const withQs = compose(
withState('search', 'setSearch', {}),
lifecycle({
componentDidMount: function () {
// works on the client side
history.listen(() => {
// history.location = Object.assign(history.location,
// parse the search string using your package of choice
// { query: parseQueryString(history.location.search) }
// )
console.log('History Location', history.location);
console.log('Search', history.location.search);
})
}
})
)
export default withQs
当history.listen
路由导航到新页面或将新查询参数添加到页面时,永远不会触发。