Home > Web Front-end > Front-end Q&A > How to request partial refresh in react

How to request partial refresh in react

藏色散人
Release: 2022-12-30 13:46:26
Original
2532 people have browsed it

React request partial refresh implementation method: 1. Introduce layout and sub-components; 2. Assign routing, code such as "const BasicRoute = () => (...)"; 3. Define the link of the project ; 4. Wrap it with the "BasicLayout" tag and pass the content to the "this.props.children" part of "layout.js".

How to request partial refresh in react

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

How to request partial refresh in react?

React implements partial refresh

[Project structure]

Process: Entry file-> Routing-> layout -> Analysi/Monitor/Workspace

1. Entry file-> src/index.js

2. Component-> src/coms

3. Layout-> src/layout

4. Routing-> src/routes

##[Process Analysis]

Process: Entry file-> Routing-> layout -> Analysi/Monitor/Workspace

1. Routing part

//import React from 'react'; 引入类
//import { Component } from 'react';  引入对象
import React, { Component } from 'react';
import {HashRouter, Route, Switch} from 'react-router-dom';

//引入布局和子组件
import BasicLayout from '../layout/layout';
import Analysis from '../coms/Analysis';
import Monitor from '../coms/Monitor';
import Workplace from '../coms/Workplace';

//分配路由
const BasicRoute = () => (
    <HashRouter>
        <Switch>
            <Route exact path="/" component={BasicLayout}/>
            <Route exact path="/Analysis" component={Analysis}/>
            <Route  path="/Monitor" component={Monitor}/>
            <Route  path="/Workplace" component={Workplace}/>
        </Switch>
    </HashRouter>
);

export default BasicRoute;
Copy after login

2. Layout part (

Key)

import React, { Component } from &#39;react&#39;;
import  {Layout ,Menu,Icon} from &#39;antd&#39;;
import { Router, Route, Link,HashRouter } from &#39;react-router-dom&#39;
import &#39;antd/dist/antd.min.css&#39;
import BasicRoute from &#39;../routes/router&#39;;

const { Header, Footer, Sider, Content } = Layout;

export default class BasicLayout extends Component {
  render() {
    return (
        <Layout>
        <Sider width={256} style={{ minHeight: &#39;100vh&#39;, color: &#39;white&#39; }}>
            
            <Menu theme="dark" mode="inline" >
                {/*定义了项目的link,会按照路由走*/}
                <Menu.Item><Link to="/Analysis">Item1</Link></Menu.Item>
            </Menu>
            
        </Sider>
        <Layout >
          <Header style={{ background: &#39;#fff&#39;, textAlign: &#39;center&#39;, padding: 0 }}>Header</Header>
          <Content style={{ margin: &#39;24px 16px 0&#39; }}>
            <div style={{ padding: 24, background: &#39;#fff&#39;, minHeight: 360 }}>
              {/*Analysis.js文件引用了BasicLayout,并把自己的全部子节点(子组件)传过来*/}
              {this.props.children}
              
            </div>
          </Content>
          <Footer style={{ textAlign: &#39;center&#39; }}>Ant Design ©2018 Created by Ant UED</Footer>
        </Layout>
      </Layout>
    )
  }
}
Copy after login

3. Subcomponent (Analysis.js) (

Error point)

import React from &#39;react&#39;;
import BasicLayout from &#39;../layout/layout&#39;;
export default () => {    //用BasicLayout标签包裹,内容传到layout.js的this.props.children部分
       return (<BasicLayout><h1>Analysis Page</h1></BasicLayout>)
}
Copy after login

[Effect]

[Summary]

Following the tutorial on the antd official website, I found that it cannot be partially refreshed

The reason is that the official website uses the umi framework. I configured it myself, but there were many omissions, resulting in subcomponents not being passed to the layout correctly.

Recommended learning: "

react video tutorial"

The above is the detailed content of How to request partial refresh in react. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template