工作是用vue,私人项目用react开发,以学习记录
import { lazy, Suspense } from 'react'
impot Loding from './components/Loading'
//1.通过React的lazy函数配合import()函数动态加载路由组件 ===> 路由组件代码会被分开打包
const Login = lazy(()=>import('@/pages/Login'))
//2.通过指定在加载得到路由打包文件前显示一个自定义loading界面
<Suspense fallback={<Loding/>}>
<Switch>
<Route path="/xxx" component={Xxxx}/>
<Redirect to="/login"/>
</Switch>
</Suspense>
详细参考文章
name={['imgUrl', index]}
这样绑定是对应 imgUrl[index]
// imgUrl: ['1111', '2222']
<Form.List name="imgUrl" >
{(fields) => (
<>
{fields.map((field, index) => (
<Form.Item
{...field}
label={"项目图片" + index}
name={['imgUrl', index]}
key={field.key}
>
<Input.Group compact>
<Input style={{ width: 'calc(100% - 100px)' }} onChange={(e) => {
form.getFieldValue('imgUrl')[index] = e.target.value;
}} />
<Button style={{ width: '50px' }} icon={<MinusOutlined />} />
<Button style={{ width: '50px' }} icon={<PlusOutlined />} />
</Input.Group>
</Form.Item>
))}
</>
)}
</Form.List>
//子组件将需要的方法暴露出ref
useImperativeHandle(ref, () => ({
open,
}));
Can‘t perform a React state update on an unmounted component This is a no-op, but it indicates a memory leak in your application...
import React, { useState, useEffect } from 'react';
const YourComponent = () => {
const [data, setData] = useState(null);
useEffect(() => {
let isMounted = true;
fetchData().then(response => {
if (isMounted) {
setData(response);
}
});
return () => {
isMounted = false;
};
}, []);
// 其他渲染逻辑
return (
// Your JSX code here
);
};
export default YourComponent;
useState
用于在函数组件中添加局部状态,并且每当状态更新时,会触发组件的重新渲染。
useRef
创建的对象中的 current 属性可以存储任何可变值,并且这个值在组件重新渲染时保持不变。
let/var
它们是用来声明变量的关键字。在函数组件中,它们声明的变量会在组件重新渲染时重新初始化为其初始值。
//权限封装代码
import React from 'react';
import { Button } from "antd";
const checkPermission = (userRole, allowedRoles) => {
return allowedRoles.includes(userRole);
};
const RoleButton = ({ userRole, allowedRoles = ['admin'], onClick, children, ...rest }) => {
const hasPermission = checkPermission(userRole, allowedRoles);
return <Button onClick={onClick} disabled={!hasPermission} {...rest} > {children} </Button>;
};
export { RoleButton}
//使用示例:
import { RoleButton } from "@/components/RoleButton"
<RoleButton userRole='admin' allowedRoles="['admin']" type='primary' style={{ marginBottom: 10 }}
onClick={() => { navTo(-1) }}>
再来一篇
</RoleButton>
react-router-dom
版本5.3.0,使用react-router-cache-route
插件替换Switch
和Route
可以实现