当前位置: 首页 > wzjs >正文

阿里云建站费用一般使用的分辨率的显示密度是多少dpi?

阿里云建站费用,一般使用的分辨率的显示密度是多少dpi?,网站项目设计说明书,网站优化主要怎么做下面,我们来系统的梳理关于 React 组件化思想 的基本知识点:一、组件化基础概念 1.1 什么是组件化? 组件化是将UI拆分为独立、可复用代码单元的开发范式。在React中,组件是构建用户界面的基本单位,每个组件封装了&…

下面,我们来系统的梳理关于 React 组件化思想 的基本知识点:


一、组件化基础概念

1.1 什么是组件化?

组件化是将UI拆分为独立、可复用代码单元的开发范式。在React中,组件是构建用户界面的基本单位,每个组件封装了:

  • 结构(JSX)
  • 样式(CSS)
  • 行为(JavaScript)
  • 状态(State)
  • 交互逻辑

1.2 为什么需要组件化?

优势说明
复用性一次开发,多处使用
可维护性独立更新不影响其他部分
分而治之复杂问题拆解为小问题
团队协作并行开发不同组件
可测试性独立单元易于测试

1.3 React组件类型

类型特点适用场景
函数组件无状态、简洁展示型组件
类组件有生命周期、状态复杂交互组件(React 16.8前主流)
Hooks组件函数组件+状态能力现代React开发首选

二、组件设计原则

2.1 单一职责原则

一个组件只做一件事:

// 反例:混合了展示和逻辑
function UserListWithActions() {// ... 数据获取、渲染列表、操作处理
}// 正例:拆分
function UserList({ users }) {// 只负责渲染
}function UserContainer() {const [users, setUsers] = useState([]);// 负责数据获取return <UserList users={users} />
}

2.2 封装与隔离

组件应隐藏内部实现细节,只暴露必要接口:

// 计数器组件
function Counter() {const [count, setCount] = useState(0);// 内部实现细节,外部无需知道const increment = () => setCount(c => c + 1);return (<div><button onClick={increment}>+</button><span>{count}</span></div>);
}

2.3 组合优于继承

React推荐使用组合而非继承构建复杂UI:

// 使用组合的弹窗组件
function Dialog({ title, children }) {return (<div className="dialog"><h2>{title}</h2><div className="content">{children}  {/* 内容由外部传入 */}</div></div>);
}// 使用
<Dialog title="警告"><p>确定要删除吗?</p><div className="actions"><button>取消</button><button>确认</button></div>
</Dialog>

三、组件通信模式

3.1 父传子:Props

function Parent() {const data = "Hello from parent";return <Child message={data} />;
}function Child({ message }) {return <p>{message}</p>;
}

3.2 子传父:回调函数

function Parent() {const handleChildEvent = (data) => {console.log("来自子组件的数据:", data);};return <Child onEvent={handleChildEvent} />;
}function Child({ onEvent }) {return (<button onClick={() => onEvent(Math.random())}>发送数据</button>);
}

3.3 兄弟组件通信

通过状态提升到共同父组件:

function Parent() {const [sharedState, setSharedState] = useState(null);return (<><SiblingA state={sharedState} setState={setSharedState} /><SiblingB state={sharedState} setState={setSharedState} /></>);
}

3.4 深层嵌套组件通信

使用Context API:

const ThemeContext = createContext('light');function App() {return (<ThemeContext.Provider value="dark"><DeepNestedComponent /></ThemeContext.Provider>);
}function DeepNestedComponent() {const theme = useContext(ThemeContext);return <div>当前主题: {theme}</div>;
}

四、组件生命周期(类组件)

4.1 生命周期图谱

挂载阶段
constructor
render
componentDidMount
更新阶段
shouldComponentUpdate
render
componentDidUpdate
卸载阶段
componentWillUnmount

4.2 关键生命周期方法

  • constructor:初始化state,绑定方法
  • componentDidMount:DOM操作、数据请求
  • shouldComponentUpdate:性能优化关键
  • componentDidUpdate:更新后DOM操作
  • componentWillUnmount:清理定时器/订阅

五、函数组件与Hooks

5.1 函数组件优势

  • 代码更简洁
  • 无this绑定问题
  • 易于测试
  • 更好的类型推断

5.2 核心Hooks

Hook作用等效生命周期
useState状态管理this.setState
useEffect副作用处理componentDidMount + componentDidUpdate + componentWillUnmount
useContext访问Contextstatic contextType
useRef引用DOM/保存可变值createRef
useMemo记忆计算结果shouldComponentUpdate
useCallback记忆函数方法绑定优化

5.3 自定义Hook

封装可复用逻辑:

function useWindowSize() {const [size, setSize] = useState({width: window.innerWidth,height: window.innerHeight});useEffect(() => {const handleResize = () => setSize({width: window.innerWidth,height: window.innerHeight});window.addEventListener('resize', handleResize);return () => window.removeEventListener('resize', handleResize);}, []);return size;
}// 使用
function MyComponent() {const { width } = useWindowSize();return <p>窗口宽度: {width}px</p>;
}

六、组件设计模式

6.1 容器组件与展示组件

容器组件展示组件
职责数据获取、业务逻辑UI呈现
复用性
状态通常有状态通常无状态
示例用户数据容器用户卡片

6.2 高阶组件(HOC)

接收组件,返回增强组件:

function withLogger(WrappedComponent) {return function(props) {useEffect(() => {console.log(`组件 ${WrappedComponent.name} 已渲染`);}, []);return <WrappedComponent {...props} />;};
}const EnhancedComponent = withLogger(MyComponent);

6.3 渲染属性(Render Props)

通过函数prop共享代码:

class MouseTracker extends React.Component {state = { x: 0, y: 0 };handleMouseMove = (e) => {this.setState({ x: e.clientX, y: e.clientY });};render() {return (<div onMouseMove={this.handleMouseMove}>{this.props.render(this.state)}</div>);}
}// 使用
<MouseTracker render={({ x, y }) => (<p>鼠标位置: {x}, {y}</p>
)} />

七、组件性能优化

7.1 避免不必要的渲染

  • React.memo:记忆函数组件
const MemoComponent = React.memo(MyComponent);
  • PureComponent:类组件浅比较
class PureComp extends React.PureComponent {}

7.2 正确使用key

// 列表项使用稳定唯一key
{items.map(item => (<ListItem key={item.id} item={item} />
))}

7.3 懒加载组件

const LazyComponent = React.lazy(() => import('./HeavyComponent'));function App() {return (<Suspense fallback={<Spinner />}><LazyComponent /></Suspense>);
}

八、组件测试策略

8.1 测试金字塔

70%20%10%测试类型分布单元测试集成测试端到端测试

8.2 测试工具

  • Jest:测试框架
  • React Testing Library:组件测试
  • Cypress:端到端测试

8.3 组件测试示例

import { render, screen, fireEvent } from '@testing-library/react';test('按钮点击后显示文本', () => {render(<Button />);fireEvent.click(screen.getByText('点击'));expect(screen.getByText('已点击')).toBeInTheDocument();
});

九、组件化最佳实践

  1. 命名规范:组件使用PascalCase,props使用camelCase
  2. 文件结构:一个组件一个文件夹,包含组件、样式、测试
components/Button/Button.jsxButton.module.cssButton.test.jsindex.js
  1. PropTypes:定义props类型(TypeScript更优)
Button.propTypes = {variant: PropTypes.oneOf(['primary', 'secondary']),onClick: PropTypes.func.isRequired
};
  1. 文档驱动开发:使用Storybook组件目录

十、组件设计实战

10.1 设计可配置按钮

function Button({children,variant = 'primary',size = 'medium',disabled = false,onClick
}) {const classNames = `btn btn-${variant} btn-${size} ${disabled ? 'disabled' : ''}`;return (<button className={classNames} disabled={disabled}onClick={onClick}>{children}</button>);
}

10.2 构建表单组件体系

function Form({ children, onSubmit }) {return <form onSubmit={onSubmit}>{children}</form>;
}function FormItem({ label, children, error }) {return (<div className="form-item"><label>{label}</label>{children}{error && <div className="error">{error}</div>}</div>);
}function Input({ ...props }) {return <input className="form-input" {...props} />;
}// 使用
<Form onSubmit={handleSubmit}><FormItem label="用户名" error={errors.username}><Input value={username} onChange={e => setUsername(e.target.value)}/></FormItem>
</Form>

总结

  • 组件是React应用的构建块,遵循单一职责原则
  • 通过Props、Context、自定义事件实现组件通信
  • 函数组件+Hooks是现代化React开发首选
  • 使用组合模式构建复杂UI
  • 通过React.memo、正确使用key等优化性能
  • 建立组件文档和测试保障质量

文章转载自:

http://o68pSYKt.gywfp.cn
http://y0Q296aV.gywfp.cn
http://2WcPAZhp.gywfp.cn
http://iZlgRv5A.gywfp.cn
http://5sCjMVRJ.gywfp.cn
http://G5tH5Xxv.gywfp.cn
http://nk2aPuNM.gywfp.cn
http://CgUBsVoP.gywfp.cn
http://m8FBZZhf.gywfp.cn
http://WWq9ZNZA.gywfp.cn
http://SZVTJssk.gywfp.cn
http://IOnhx9my.gywfp.cn
http://VyWUtJfD.gywfp.cn
http://YNDt1gKs.gywfp.cn
http://P2fP8vc1.gywfp.cn
http://I3fmp1jS.gywfp.cn
http://2tl7tWiu.gywfp.cn
http://Qax5exym.gywfp.cn
http://CgjkCszN.gywfp.cn
http://FIoAcPJE.gywfp.cn
http://QYYLiYph.gywfp.cn
http://SXhadYhw.gywfp.cn
http://CQ7xmUFq.gywfp.cn
http://hdn7XvQ2.gywfp.cn
http://qkNhmnH7.gywfp.cn
http://PVhCQ5TR.gywfp.cn
http://EsMWpss3.gywfp.cn
http://wVyXD8bv.gywfp.cn
http://2FSQccxg.gywfp.cn
http://LtNJNkxF.gywfp.cn
http://www.dtcms.com/wzjs/678431.html

相关文章:

  • 做墙报的网站建设网站需要哪些流程
  • 网站建设颊算网络公司主要做哪些
  • 网站建设注意什么国外网站 服务器
  • 中英文双语网站站点如何做好一个企业网站
  • 企业建网站哪家好创一家网站
  • 佛山选择免费网站优化wordpress只显示文本摘要
  • 网站打不开被拦截怎么办网页制作购物网站
  • 做衣服的网站推荐福田欧曼银河报价
  • 手工做衣服网站有哪些广州哪里有外贸网站
  • 怎样建设网站教程使用网站模板侵权吗
  • 学校网站 建设常德网站公司
  • 东平可信的网站建设短链接生成网
  • 自己有网站 做app太仓公司网站建设电话
  • 做网站的要多钱wordpress数据表格
  • 一键生成海报的网站wordpress如何防注入
  • 网页设计与编程福州专业的seo软件
  • wordpress模板怎么安装教程视频做网站优化的
  • 坪山网站建设公司东莞横沥中学
  • 陕西省住建厅网站官网做食品的网站设计要注意
  • 浪子做的阿哲喊麦网站多少wordpress手机移动主题
  • 云主机建站网站建设具体详细过程
  • 专业商城网站建设多少钱centos支持wordpress
  • 网站建设规划书河北专门做办公的网站
  • 河南省住房城乡和建设厅网站首页wordpress 指南
  • 网站如何做微信支付链接建设个人网站教程
  • 一点空间网站建设wordpress启动慢
  • 建设网站报价单手机网站 建设
  • 三合一网站什么意思注册深圳公司的好处
  • 西部数码官方网站建设工程教育app
  • 建设网站需要花多少钱国家建设部网站2018年