react重新渲染菜单_react-router 页面刷新,但是菜单栏的active效果重置的问题

当使用React和react-router时,遇到页面刷新导致菜单的active效果重置问题。文章通过分享代码片段展示了如何创建菜单,并提出了解决方案:在URL中添加参数,以便在重新渲染时根据该参数保持菜单的激活状态。
摘要由CSDN通过智能技术生成

1.页面刷新,但是active效果会重置回第一个

2.下面是代码

const menu = [

{menuUrl: 'console', menuName: '控制台'},

{menuUrl: 'loanBefore', menuName: '进件管理'},

{menuUrl: 'loanLater', menuName: '贷后'},

{menuUrl: 'customer', menuName: '客户'},

{menuUrl: 'operationManager', menuName: '运营管理'},

{menuUrl: 'application', menuName: '应用'},

{menuUrl: 'set', menuName: '设置'},

{menuUrl: 'detail', menuName: '数据'}

]

export default class Content extends Component {

constructor(props) {

super(props)

this.state = {

bgColor: 0

}

//this.changeBgColor = this.changeBgColor.bind(this);

}

changeBgColor(index) {

this.setState({

bgColor: index

})

}

render() {

var {url} = this.props.match;

return (

<div>

<div className="" style={{overflow:'hidden'}}>

{/*<!--此处作为更新视图容器-->*/}

<div className="nav-bg">

<div className="nav-left floatLeft">

<div className="nav-left-icon floatLeft"></div>

<div className="nav-left-title floatLeft">凯盛金融</div>

<div className="nav-left-bor floatLeft"></div>

<div className="nav-left-name floatLeft">金融管理系统</div>

</div>

<div className="nav-center floatLeft">

<ul id="nav-parent">

{

menu.map((item, index) =>

(<li key={index} className={`index ${this.state.bgColor === index ? "actives" : ""}`}

onClick={this.changeBgColor.bind(this, index)}>

<Link className="js-menu-item"

to={`${url}/${item.menuUrl}`}>{item.menuName}</Link>

</li>)

)

}

</ul>

</div>

</div>

</div>

<Switch>

<Route path={`${url}/console`} component={Console}/>

<Route path={`${url}/loanBefore`} component={LoanBefore}/>

<Route path="/console" component={Console}/>

<Route path="/console" component={Console}/>

<Route path="/console" component={Console}/>

<Route path="/console" component={Console}/>

</Switch>

</div>

)

}

3.刷新后active效果如何不重置,想到了在url里加个参数,重新渲染的时候拿个这个参数去给对应的tab active效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用react-router-dom创建动态路由非常简单。我们可以使用Route组件来定义路由并在其中使用动态参数。以下是如何使用react-router-dom创建动态路由的步骤: 1. 安装react-router-dom:使用以下命令安装react-router-dom。 ``` npm install react-router-dom ``` 2. 在项目中导入所需的组件: ```javascript import React from "react"; import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; ``` 3. 创建路由并在其中使用动态参数: ```javascript <Router> <Switch> <Route exact path="/" component={Home} /> <<Route exact path="/users/:id" component={User} /> </Switch> </Router> ``` 上面的代码中,我们定义了一个具有动态参数的路由。用户可以通过访问URL `/users/:id` 来访问该路由。其中,`:id` 是一个动态参数,它将被替换为用户请求的实际值。 4. 在组件中获取动态参数: 在上面的代码中,我们定义了一个名为`User`的组件。我们可以在该组件中通过`props.match.params.id`来获取动态参数的值。以下是一个简单的示例: ```javascript import React from "react"; const User = (props) => { return <h1>User ID: {props.match.params.id}</h1>; }; export default User; ``` 在上面的代码中,我们使用`props.match.params.id`来获取动态参数的值,并将其显示在页面上。 这就是使用react-router-dom创建动态路由的基本步骤。你可以根据你的具体需求进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值