codesandbox https://codesandbox.io/s/9l6prnyxjy

app.js

import React, { Component, Fragment } from "react";
import {
AppBar,
Button,
Tabs,
Tab,
Icon,
Typography,
Fade,
Slide
} from "@material-ui/core";
import _ from "lodash";
import {
BrowserRouter,
HashRouter,
Link,
Redirect,
Route,
Switch,
withRouter
} from "react-router-dom"; const l = console.log; let Home = props => {
return (
<Fragment>
<Typography variant="headline">首页</Typography>
</Fragment>
);
};
let About = props => {
return (
<Fragment>
<Typography variant="headline">关于</Typography>
</Fragment>
);
}; let Mine = props => {
return (
<Fragment>
<Typography variant="headline">我的</Typography>
</Fragment>
);
}; @withRouter
class Tabbars extends Component {
state = {
tabs: [
{
label: "home",
to: "/home",
icon: "home"
},
{
label: "about",
icon: "supervised_user_circle",
to: "/about"
},
{
label: "mine",
icon: "perm_identity",
to: "/mine"
}
],
value: 0
};
handleChange = (event, value) => {
this.setState({ value });
};
toNav = to => e => {
this.props.history.push(to);
}; componentWillMount() {
// l(this.props)
let { location, history } = this.props; // 确保用户在浏览器改变路由,激活按钮发生变化
this.changeTabbarValue(location.pathname); // 监听路由的变化,主要用于重定向时确保激活按钮发生变化
history.listen(({ pathname }, action) => {
l("router change");
// action === "REPLACE" &&
this.changeTabbarValue(pathname);
});
} changeTabbarValue(pathname) {
let i = this.state.tabs.findIndex(({ to }) => to.includes(pathname));
if (i < 0) {
return l("没找到根路由");
}
i !== this.state.value &&
this.setState({
value: i
});
} componentDidMount() {
document.title = "Ajanuw";
} render() {
return (
<AppBar position="static" color="default">
<Tabs
value={this.state.value}
onChange={this.handleChange}
indicatorColor="primary"
textColor="primary"
fullWidth
>
{this.state.tabs.map(($_, index) => {
return (
<Tab
label={$_.label}
key={index}
onClick={this.toNav($_.to)}
icon={<Icon>{$_.icon}</Icon>}
/>
);
})}
</Tabs>
</AppBar>
);
}
} // Tabbars = withRouter(Tabbars) class App extends Component {
render() {
return (
<BrowserRouter>
<Fragment>
<Tabbars />
{/* 路由中有重定向, 加入动画效果可能会报错 */}
<Switch>
{/* <Route
exact
strict
path="/"
render={props => true && (<Redirect to='/home'/>)}
/> */}
{/* <Redirect exact from='/' to='/home'/> */}
<Redirect from="/" to="/home" exact strict />
<Route
exact
strict
path="/home"
render={props => <Home {...props} />}
/>
<Route path="/about" component={About} />
<Route path="/mine" component={Mine} />
<Route
render={() => {
return <div>404</div>;
}}
/>
</Switch>
</Fragment>
</BrowserRouter>
);
}
} export default App;

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import CssBaseline from "@material-ui/core/CssBaseline";
import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";
import { pink, blue } from "@material-ui/core/colors"; // 自定义主题
const theme = createMuiTheme({
palette: {
primary: blue,
secondary: pink
}
}); ReactDOM.render(
<React.Fragment>
<CssBaseline />
<MuiThemeProvider theme={theme}>
<App />
</MuiThemeProvider>
</React.Fragment>,
document.getElementById("root")
);

react 路由导航栏 withRouter的更多相关文章

  1. React Navigation 导航栏样式调整+底部角标消息提示

    五一佳节匆匆而过,有人选择在外面看人山人海,有人选择宅在家中度过五一,也有人依然坚守在第一线,致敬! 这是坚持学习react-native的第二篇文章,可能会迟到,但是绝不会缺席,这篇要涉及到的是re ...

  2. 【React -- 9/100】 抽离顶部导航栏 - [组件复用]

    今天写的页面中需要重复使用到顶部导航栏,所以把顶部导航栏抽离出来 考虑复用组件的健壮性,使用PropTypes校验,可以自定义一个click事件 JSX import React from " ...

  3. 在React中使用 react-router-dom 编程式路由导航的正确姿势【含V5.x、V6.x】

    ## react-router-dom 编程式路由导航 (v5) ###### 1.push跳转+携带params参数 ```jsx props.history.push(`/b/child1/${i ...

  4. element-ui使用导航栏跳转路由用法

    element-ui使用导航栏跳转路由用法 最近初学vue,试着做一个小项目熟悉语法与思想,其中使用elemen-ui的导航栏做路由跳转切换页面.下面记录一下学习过程 element-ui引入vue项 ...

  5. React Native(四)——顶部以及底部导航栏实现方式

    效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...

  6. elementUI的导航栏怎么根据路由默认选中相关项

    1. <el-menu :default-active="this.$route.path.substr(1)" class="left-nav"> ...

  7. vue+el-menu实现路由刷新和导航栏菜单状态保持(局部刷新页面)

    一.菜单项激活状态保持 有时,我们在项目中会有这样一个需求,即实现 一个侧导航栏,点击不同的菜单项,右边内容会跟着变化,而页面手动刷新后想要使菜单激活状态保持,那么这个功能该如何实现呢? 现在给出以下 ...

  8. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

  9. React实现顶部固定滑动式导航栏(导航条下拉一定像素时显示原导航栏样式)

    摘要 基于react的框架开发一个顶部固定滑动式的酷炫导航栏,当导航栏置顶时,导航栏沉浸在背景图片里:当鼠标滑动滚轮时,导航栏固定滑动并展示下拉样式. JS部分 相关技术栈:react.antd.re ...

随机推荐

  1. Unity3d如何profile模拟器

    最近有反馈X2在一些模拟器中运行偶尔非常卡,达到5秒左右,而这类问题在真机上没出现过,于是想用unity profile下模拟器.但模拟器是运行在虚拟机里面的,市面上大多模拟器并没有提供虚拟机网络设置 ...

  2. mysql -- this is incompatible with sql_mode=only_full_group_by

    select @@GLOBAL.sql_mode; set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER ...

  3. 解决Spring Boot OTS parsing error: Failed to convert WOFF 2.0

    <build> <resources> <resource> <directory>${project.basedir}/src/main/resour ...

  4. ES6 与 React

    Node和NPM/*安装npm*/npm installnpm install <package>npm install -g <package> /*更新安装包*/npm u ...

  5. Nginx 反向代理504 Gateway Time-out

    location /ssfwpt { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_ ...

  6. 评分模型的检验方法和标准&信用评分及实现

    评分模型的检验方法和标准通常有:K-S指标.交换曲线.AR值.Gini数等.例如,K-S指标是用来衡量验证结果是否优于期望值,具体标准为:如果K-S大于40%,模型具有较好的预测功能,发展的模型具有成 ...

  7. Android 6.0 动态权限申请注意事项

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 And ...

  8. 详解Docker的网络模式

    我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用--net=host指定container模式:使用- ...

  9. Linux Shell函数返回值

    转:http://blog.csdn.net/ithomer/article/details/7954577 Shell函数返回值,一般有3种方式:return,argv,echo 1) return ...

  10. Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 v4

    Atitit.如何文章写好 论文  文章  如何写好论文 技术博客 1. 原则 2 1.1. 有深度, 有广度 2 1.2. 业务通用性有通用性 尽可能向上抽象一俩层..业务通用性与语言通用性. 2 ...