惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - thanks

07-紧固件MES系统 — 中小企业实施部署指南 06-紧固件MES系统 — 术语与名词解析 05-紧固件MES系统 — 推荐技术架构与智能制造方案 03-紧固件MES系统 — 模块业务详解 04-紧固件MES系统 — 重构建议与待开发分析 01-紧固件MES系统 — 系统总览与架构 02-紧固件MES系统 — 核心业务流程 VsCode+DeepSeek的AI编程助手初体验 [大数据][机器学习]之Model Card(模型卡片)介绍 Bond——大数据时代的数据交换和存储格式 Spark中的那些坑... 为啥就业这么难——聊聊我在培训机构的所见所闻 微软外服工作札记③——窗口函数的介绍 微软外服工作札记②——聊聊微软的知识管理服务平台和一些编程风格 【非技术】试占12月10日后会发生什么 聊聊我在微软外服大数据分析部门的工作经历及一些个人见解 [工作札记]03: 微软Winform窗体中设计上的Bug,会导致程序编译失败,影响范围:到最新的.net4.7.2都有 [工作札记]02: .Net Winform控件TreeView最简递归绑定方法 SAAS云平台搭建札记: (三) AntDesign + .Net Core WebAPI权限控制、动态菜单的生成 [工作札记]01: CS系统中分页控件的制作 【非技术】试占新型肺炎的情况与发展趋势 给培训学校讲解ORM框架的课件 SAAS云平台搭建札记: (二) Linux Ubutu下.Net Core整套运行环境的搭建 SAAS云平台搭建札记: (一) 浅论SAAS多租户自助云服务平台的产品、服务和订单 开源三维地球GIS引擎Cesium常用功能的开发 Asp.net管理信息系统中数据统计功能的实现 - thanks
SAAS云平台搭建札记: (四) AntD For React使用react-router-dom路由接收不同参数页面不刷新的问题
thanks · 2021-04-26 · via 博客园 - thanks

    在.net开发员眼里,如果使用MVC,根据路由匹配原则,可以通过各种方式接收参数,比如 /Post/List/1, /Post/List/2,或者 /Post/List?id=1,/Post/List?id=2,后端PostController中的List Action都能接收到id为1或者2的参数,进行相应操作;但是,我们使用Ant Design For React的时候,如果使用react-router-dom作为路由,不管使用哪种参数,点击不同参数页面链接的时候,地址栏里页面参数是变了,但是后台页面居然不刷新,还是维持原先的页面,这是怎么一回事呢,我们来一探究竟。

    我们的思路是:在P_Index页面里面使用js获取地址栏参数的方法,获取参数,进行页面渲染,代码如下:

Main.js (主要的框架页面,我们要渲染P_Index)

......
import { BrowserRouter as Router, Switch, Route, Link} from "react-router-dom";
import P_Index from './p/Index';
......
class Main extends React.Component {
...... render() {
return ( <Router> <Layout> <Sider collapsible collapsed={this.state.collapsed}> ...... <Switch> <Route path="/p"> <P_Index></P_Index> </Route> ....... </Switch> </Content> </Layout> </Layout> </Router> ); } } export default Main;

/p/Index.js (负责接收参数进行渲染的页面)

import React from 'react';
......

export default class P_Index extends React.Component {

    //页面参数
    pageId = window.location.pathname.split('/')[2];
    //pageId = window.location.search.substr(1);
async componentDidMount() { let data
=  await getData(pageId); this.setState({...data}); } render() { if(this.state.type){ switch(this.state.type){ case "aaa": return ( ...... ); case "bbb": return ( ....... ); } }else{ return ('loading') } } }

    代码也很简单,页面加载时根据地址栏参数查不同数据,改变state,然后根据获取的数据进行渲染。

    但是,我们在地址栏里不管是输入 /p/1 还是 /p/2 或者 /p/3,后面的页面丝毫不变化,都是第一次渲染的页面。页面内打断点,跟踪,发现地址栏改变了页面根本就没有重新渲染。我们又尝试了 /p?1 、/p?2 或者 /p?id=1、/p?id=2,都一样。这可难倒我们了,不管是html、.net WebForm、MVC,地址栏一变,页面不要重新渲染的吗?为什么在react里面就不一样了呢?

 (输入不同地址页面内容竟然一样,而参数在第一次加载页面的时候已经拿到了) 

    抱着这个疑问,我们查询了官方的react-router-dom文档,然后根据文档结合项目,最终找到了解决的方法,摒弃了在页面里使用 window.location.pathname 或者 window.location.search 方式取参数的方法,直接在路由中配置。

    改进后的文件如下:

Main.js

......
import { BrowserRouter as Router, Switch, Route, Link, useParams} from "react-router-dom";
import P_Index from './p/Index';
......
function ShowPage () { let { id } = useParams(); console.log(id); return <P_Index id={id}></P_Index>; }; class Main extends React.Component { ...... render() { return ( <Router> <Layout> <Sider collapsible collapsed={this.state.collapsed}> ...... <Switch> <Route path="/p/:id"> <ShowPage></ShowPage> </Route> ...... </Switch> </Content> </Layout> </Layout> </Router> ); } } export default Main;

/p/Index.js

import React from 'react';
......
export default class P_Index extends React.Component {
    constructor(props) {
        super(props);
    }
......
    componentWillReceiveProps = async (nextProps) => {
        const { id } = this.props;
        if (id !== nextProps.id) {
            let data = await getData(nextProps.id);
            this.setState({ ...data });
        }
    }

    //页面参数
    //pageId = window.location.pathname.split('/')[2];
    //pageId = window.location.search.substr(1);

    async componentDidMount() {
        let data = await getData(this.props.id);
        this.setState({ ...data });
    }
......
    render() {
        if (this.state.type) {
            switch (this.state.type) {
                case "aaa":
                    return (
                        ......
                    );
                case "bbb":
                    return (
                        ......
                    );
            }
        } else {
            return ('loading')
        }
    }
}

    我们增加了一个componentWillReceiveProps方法,该方法在每次属性改变时都会执行,这下通过菜单点击,只要地址栏里参数变化,就算是同一个页面,页面也会进行刷新,数据也会发生相应变化。至此,问题完美解决!

    SAAS云平台搭建札记系列文章:

    SAAS云平台搭建札记: (一)浅论SAAS多租户自助云服务平台的产品、服务和订单

    SAAS云平台搭建札记: (二)Linux Unbutu下.Net Core整套运行环境的搭建

    SAAS云平台搭建札记: (三) AntDesign + .Net Core WebAPI权限控制、动态菜单的生成

    SAAS云平台搭建札记: (四) AntD For React使用react-router-dom路由接收不同参数页面不刷新的问题