React Router 中文文档(二)
官方英文文档 - https://reacttraining.com/rea... history本文档中的术语 以下术语我们会经常使用:
history is mutable
class Comp extends React.Component { componentWillReceiveProps(nextProps) { // locationChanged 会是 true const locationChanged = nextProps.location !== this.props.location; // 错误,locationChanged 永远是 false,因为 history 是可变的。 const locationChanged = nextProps.history.location !== this.props.history.location; } } <Route component={Comp} /> 根据你使用的实现方式,还可能存在其它属性。有关详细信息,请参阅 history 文档。 location
{ key: 'ac3df4',// 使用 hash history 时,没有这个属性 pathname: '/somewhere' search: '?some=search-string',hash: '#howdy',state: { [userDefined]: true } } Router 将在以下几个地方为您提供一个
componentWillReceiveProps(nextProps) { if (nextProps.location !== this.props.location) { // 已经跳转了! } } 可以在以下不同情境中使用
通常情况下只是使用一个字符串,但是如果你需要添加一些额外的 // 通常情况下我们这么做 <Link to="/somewhere" /> // 但是我们可以改为使用 location 对象 const location = { pathname: '/somewhere',state: { fromDashboard: true } }; <Link to={location} /> <Redirect to={location} /> history.push(location); history.replace(location); 最终,
这将阻止它们在 Router 状态下使用实际位置。这对动画和等待导航非常有用,或者任何时候你想诱导一个组件在不同于真实位置的地方渲染。 match一个
您可以在以下几个地方访问
如果 null matches在 解析 URL 的默认方式是将 `${match.url}/relative-path` 如果你在 当您在产生空匹配对象的 // location.pathname = '/matches' <Route path='/does-not-match' children={({ match }) => ( // match === null <Route render={({ match: pathlessMatch }) => ( // pathlessMatch === ??? )} /> )} /> 没有
matchPath在正常的渲染周期之外,你可以使用和 import { matchPath } from 'react-router'; const match = matchPath('/users/123',{ path: '/users/:id',exact: true,strict: false }); pathname第一个参数是要匹配的路径名。如果您在服务器上通过 Node.js 使用,它将是 props第二个参数是匹配的属性,它们与 { path,// 例如 /users/:id strict,// 可选,默认为 false exact // 可选,默认为false } withRouter你可以通过 import React from 'react'; import PropTypes from 'prop-types'; import { withRouter } from 'react-router-dom'; // 显示当前位置的 pathname 的简单组件 class ShowTheLocation extends React.Component { static propTypes = { match: PropTypes.object.isRequired,location: PropTypes.object.isRequired,history: PropTypes.object.isRequired } render() { const { match,location,history } = this.props; return ( <div>You are now at {location.pathname}</div> ); } } // 创建一个连接到 Router 的新组件(借用 redux 术语) const ShowTheLocationWithRouter = withRouter(ShowTheLocation) 注意:withRouter 不会订阅位置更改,如 React Redux 的 connect 对状态更改所做的更改。而是在位置更改从 <Router> 组件传播出去之后重新呈现。这意味着除非其父组件重新呈现,否则使用 withRouter 不会在路由转换时重新呈现。如果使用 withRouter 来防止更新被 shouldComponentUpdate 阻塞,那么使用router 包装实现 shouldComponentUpdate 的组件是非常重要的。例如,使用 Redux 时: // This gets around shouldComponentUpdate withRouter(connect(...)(MyComponent)) // or compose( withRouter,connect(...) )(MyComponent) // This does not connect(...)(withRouter(MyComponent)) // nor compose( connect(...),withRouter )(MyComponent) 有关更多信息,请参阅本指南。 静态方法和属性 封装组件的所有无反应的特定静态方法和属性都会自动复制到 connected 组件。 Component.WrappedComponent被包装的组件被公开为返回组件上的静态属性 // MyComponent.js export default withRouter(MyComponent); // MyComponent.test.js import MyComponent from './MyComponent'; render(<MyComponent.WrappedComponent location={{...}} ... />); wrappedComponentRef: func一个将作为 class Container extends React.Component { componentDidMount() { this.component.doSomething(); } render() { return ( <MyComponent wrappedComponentRef={c => this.component = c} /> ) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |