React-Router 杂记

栏目: IOS · Android · 发布时间: 7年前

内容简介:react-router和redux问题有时候,当location改变,组件并没有更新(子路由组件或者activity link),主要是因为:原因是redux内部实现了shouldComponentUpdate,但又没有从react-router接收到props,意味着不会改变。解决办法:

三种Router的区别

1. HashRouter: 即对应url中的hash值,如xx.com/#/a、xx.com/#/a/b, 服务器对任务url都返回同一个url,具体的路径由浏览器区分,因为浏览器不会发送hash后面的值给服务器。  
2. BrowserRouter:如果是BrowseRouter即url变成这样,xx.com/a、xx.com/a/b, 所以要对服务器配置不同的url返回不同的资源。
3. MemoryRouter: 就是没有URL的情况,比如(React Native)。
复制代码

react-router的哲学

github.com/rccoder/blo…
1. 动态路由,每一个route都是一个组件,更好的配合React  
2. 路由嵌套
复制代码

react-router和redux问题

有时候,当location改变,组件并没有更新(子路由组件或者activity link),主要是因为:

1.组件直接通过redux的connect  
2.该组件不是路由组件,也就是没有这样的代码
复制代码
<Route component={SomeConnectedThing}/>
复制代码

原因是redux内部实现了shouldComponentUpdate,但又没有从react-router接收到props,意味着不会改变。解决办法:

// before
export default connect(mapStateToProps)(Something)

// after
import { withRouter } from 'react-router-dom'
export default withRouter(connect(mapStateToProps)(Something))
复制代码

以上所述就是小编给大家介绍的《React-Router 杂记》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具