当多个<router />使用同一个组件的时候,切换页面地址,页面不刷新的问题
2017-11-14 11:43
891 查看
如标题所说的问题,更详细一点就是多个router使用同一个react页面组件的时候,当切换路由的时候,页面组件不重新构建,页面也不刷新。
例如:
以上情况当路由切换的时候,组件
路由不需要修改,我们只需要修改组件:
上面提到需要给组件加上key,让
这里的
这样切换路由的时候,即可完成组件的刷新(重构)。
例如:
router.js
import Index from './routes/Index/index'; import UserAccount from './routes/UserAccount/index'; const Root = () =>( <Router history={history} > <div style={{ height:'100%' }}> <Route exact path='/' component={Login} /> <Route path="/sem" component={Layout}> <Route path="Index" component={Index} /> <Route path="UserAccount" component={UserAccount} /> <Route path="UserAccountForMaster" component={UserAccount} /> <Route path="UserAccountForNormal" component={UserAccount} /> </Route> </div> </Router> ) ReactDOM.render(<Root />,document.getElementById('root'))
UserAccount.js
import React from 'react'; export default class UserAccount extends React.Component { constructor (props) { super(props); this.state = { dataList:[] } } componentWillMount () { } componentDidMount () { sendFetch('http://www.baidu.com', {type:1}, 'GET') .then(data=>{ this.setState({ listData:data.data.lists }) }) } render () { return ( <div> <button>你好,祝你幸福,再见~</button> <ul> { listData.length !== 0 ? listData.map((item,index)=>{ return <li>{item.name}</li> }) : null } </ul> </div> ) } }
以上情况当路由切换的时候,组件
UserAccount只是重新render,并不重新构建,如果需要路由切换的时候,组件重新构建,重新完成一次生命周期,则需要给组件加上
key;方法如下:
·····这是解决办法·····这是解决办法·····这是解决办法·····这是解决办法·····这是解决办法·····这是解决办法·····
路由不需要修改,我们只需要修改组件:
上面提到需要给组件加上key,让
router的
path属性(params)指向组件的key,就可以实现,组件的重构。
UserAccount.js
import React from 'react'; // 注意这里 class UserAccount extends React.Component { constructor (props) { super(props); this.state = { dataList:[] } } componentWillMount () { } componentDidMount () { sendFetch('http://www.baidu.com', {type:1}, 'GET') .then(data=>{ this.setState({ listData:data.data.lists }) }) } render () { return ( <div> <button>你好,祝你幸福,再见~</button> <ul> { listData.length !== 0 ? listData.map((item,index)=>{ return <li>{item.name}</li> }) : null } </ul> </div> ) } } // 注意这里 export default (props)=><UserAccount {...props} key={props.location.pathname} />
这里的
props.location.pathname就是router的path属性的值,这样就实现了,router的path属性指向组件的key。
这样切换路由的时候,即可完成组件的刷新(重构)。
相关文章推荐
- Jsp页面关于BootStrap模态框和<a href onclick>事件不能同时使用,以及传参的问题
- 使用yii的layout,加入<?php echo $content; ?>这句话时,它会自动在子页面上面添加一个div包裹
- react-router 与react-reduct 配合使用时,页面不刷新问题
- 使用yii的layout,加入<?php echo $content; ?>这句话时,它会自动在子页面上面添加一个div包裹
- jsp中使用<jsp:include />在切换tomcat版本时遇到的问题
- 使用<a>标签,连接到另外一个页面
- display不能解决<jsp:include />的问题,jsp异步加载另外一个jsp页面
- 记录<a>标签使用中click事件中ajax提交数据后,整体页面也做了刷新操作的坑
- 【微信小程序】解决 竖向<scroll-view>组件 “竖向滚动页面出现遮挡”问题
- 关于一个页面的tab切换整体页面刷新而tab标签处是同一个文件怎么做焦点的问题
- Apache_通常每个套接字地址<协议>只允许使用一次_80端口问题
- react-router(v3)切换页面时不刷新页面,实现显示和隐藏子路由组件
- react-router与react-reduct配合使用时页面不刷新问题解决方法
- 分页列表td中包含有<标签导致页面显示错乱问题,使用jstl <c:out>标签秒解决
- 解决element-ui中el-menu组件作为vue-router模式在刷新页面后default-active属性与当前路由页面不一致问题的方法
- Mybatis 使用<where>标签时遇到的一个问题与<trim>标签的使用
- ASP.NET母版页使用 按下回车刷新页面 去掉 <form></form>标签
- jsp页面,使用Struts2标签,传递和获取Action类里的参数,注意事项。<s:a action><s:iterator><s:param>ognl表达式 在编写SSH2项目的时候,除
- vue-router中关于组件复用页面不刷新的问题
- angular2中router路由跳转navigate的使用与刷新页面问题详解