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".
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/Workspace1. 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;
Key)
import React, { Component } from 'react'; import {Layout ,Menu,Icon} from 'antd'; import { Router, Route, Link,HashRouter } from 'react-router-dom' import 'antd/dist/antd.min.css' import BasicRoute from '../routes/router'; const { Header, Footer, Sider, Content } = Layout; export default class BasicLayout extends Component { render() { return ( <Layout> <Sider width={256} style={{ minHeight: '100vh', color: 'white' }}> <Menu theme="dark" mode="inline" > {/*定义了项目的link,会按照路由走*/} <Menu.Item><Link to="/Analysis">Item1</Link></Menu.Item> </Menu> </Sider> <Layout > <Header style={{ background: '#fff', textAlign: 'center', padding: 0 }}>Header</Header> <Content style={{ margin: '24px 16px 0' }}> <div style={{ padding: 24, background: '#fff', minHeight: 360 }}> {/*Analysis.js文件引用了BasicLayout,并把自己的全部子节点(子组件)传过来*/} {this.props.children} </div> </Content> <Footer style={{ textAlign: 'center' }}>Ant Design ©2018 Created by Ant UED</Footer> </Layout> </Layout> ) } }
Error point)
import React from 'react'; import BasicLayout from '../layout/layout'; export default () => { //用BasicLayout标签包裹,内容传到layout.js的this.props.children部分 return (<BasicLayout><h1>Analysis Page</h1></BasicLayout>) }
[Summary]Following the tutorial on the antd official website, I found that it cannot be partially refreshedThe 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!