
reactjs 中的设计模式为应用程序开发中的常见问题提供了标准化且经过验证的解决方案。使用这些模式不仅使您的代码更具可读性和可维护性,而且还增强了其可扩展性和健壮性。让我们深入研究一些最流行的 reactjs 设计模式,并通过示例来说明它们的用法。
1. 容器和表示组件模式
容器和展示模式将组件分为两类:
展示组件:关注事物的外观(ui)。容器组件:关注事物如何工作(逻辑和状态管理)。
这种分离可以实现更好的可重用性、更容易的测试和更清晰的代码。
示例:展示和容器组件
// presentational component: displaying user list (userlist.js)import react from 'react';const userlist = ({ users }) => ( - {users.map((user) => (
- {user.name} ))}
// container component: fetching user data (usercontainer.js)import react, { usestate, useeffect } from 'react';import userlist from './userlist';const usercontainer = () => { const [users, setusers] = usestate([]); useeffect(() => { const fetchusers = async () => { const response = await fetch('https://jsonplaceholder.typicode.com/users'); const data = await response.json(); setusers(data); }; fetchusers(); }, []); return ;};export default usercontainer;
这里,userlist 是一个展示组件,它接收用户作为 props,而 usercontainer 则处理数据获取和状态管理。
2. 高阶组件(hoc)模式
a 高阶组件 (hoc) 是一个将组件作为参数并返回新组件的函数。 hoc 通常用于横切关注点,例如身份验证、日志记录或增强组件行为。
示例:创建 hoc 进行授权
// withauthorization.js (hoc for authorization)import react from 'react';const withauthorization = (wrappedcomponent) => { return class extends react.component { componentdidmount() { if (!localstorage.getitem('authtoken')) { // redirect to login if not authenticated window.location.href = '/login'; } } render() { return ; } };};export default withauthorization;
// dashboard.js (component wrapped with hoc)import react from 'react';import withauthorization from './withauthorization';const dashboard = () =>welcome to the dashboard
;export default withauthorization(dashboard);
通过使用 withauthorization 包装 dashboard,您可以确保只有经过身份验证的用户才能访问它。
3. 渲染道具模式
render props 模式涉及使用值为函数的 prop 在组件之间共享代码。此模式对于基于特定条件或状态的动态渲染非常有用。
示例:使用渲染道具进行鼠标跟踪
// mousetracker.js (component with render props)import react, { usestate } from 'react';const mousetracker = ({ render }) => { const [position, setposition] = usestate({ x: 0, y: 0 }); const handlemousemove = (event) => { setposition({ x: event.clientx, y: event.clienty }); }; return {render(position)};};export default mousetracker;
// app.js (using render props)import react from 'react';import mousetracker from './mousetracker';const app = () => ( (mouse position: ({x}, {y})
)} />);export default app;
mousetracker 组件使用 render prop 将鼠标位置数据传递给任何组件,使其具有高度可重用性。
4. 自定义挂钩模式
自定义 hook 允许您跨多个组件封装和重用有状态逻辑。这种模式促进了代码的可重用性和清晰的关注点分离。
示例:创建用于获取数据的自定义挂钩
// usefetch.js (custom hook)import { usestate, useeffect } from 'react';const usefetch = (url) => { const [data, setdata] = usestate(null); const [loading, setloading] = usestate(true); useeffect(() => { const fetchdata = async () => { const response = await fetch(url); const result = await response.json(); setdata(result); setloading(false); }; fetchdata(); }, [url]); return { data, loading };};export default usefetch;
// app.js (using the custom hook)import react from 'react';import usefetch from './usefetch';const app = () => { const { data, loading } = usefetch('https://jsonplaceholder.typicode.com/posts'); if (loading) return loading...; return ( - {data.map((post) => (
- {post.title} ))}
usefetch 自定义钩子封装了数据获取逻辑,可以在不同组件之间复用。
5. 复合组件模式
复合组件模式允许组件一起工作来管理状态和行为。此模式对于构建复杂的 ui 组件(如选项卡、手风琴或下拉菜单)非常有用。
示例:使用复合组件构建选项卡
// tabs.js (parent component)import react, { usestate } from 'react';const tabs = ({ children }) => { const [activeindex, setactiveindex] = usestate(0); return react.children.map(children, (child, index) => react.cloneelement(child, { isactive: index === activeindex, setactiveindex, index }) );};const tab = ({ children, isactive, setactiveindex, index }) => ( );const tabpanel = ({ children, isactive }) => (isactive ? {children} : null);tabs.tab = tab;tabs.tabpanel = tabpanel;export default tabs;
// app.js (using compound components)import react from 'react';import tabs from './tabs';const app = () => ( tab 1 tab 2 content for tab 1 content for tab 2 );export default app;
tabs 组件管理状态,而 tab 和 tabpanel 组件协同显示选项卡内容。
6. 受控和非受控组件模式
受控组件完全由 react 状态管理,而非受控组件则依赖 dom 来获取状态。两者都有其用途,但为了一致性和可维护性,受控组件通常是首选。
示例:受控组件与不受控组件
// controlled component (textinputcontrolled.js)import react, { usestate } from 'react';const textinputcontrolled = () => { const [value, setvalue] = usestate(''); return ( setvalue(e.target.value)} /> );};export default textinputcontrolled;
// uncontrolled component (textinputuncontrolled.js)import react, { useref } from 'react';const textinputuncontrolled = () => { const inputref = useref(); const handleclick = () => { console.log(inputref.current.value); }; return ( > );};export default textinputuncontrolled;</pre>在受控组件中,react 完全控制表单状态,而在非受控组件中,状态由 dom 本身管理。
7. 钩子工厂模式
hooks 工厂模式涉及创建动态生成和管理多个状态或行为的钩子,提供灵活的方式来管理复杂逻辑。
示例:使用 hooks factory 进行动态状态管理
// usedynamicstate.js (hook factory)import { usestate } from 'react';const usedynamicstate = (initialstates) => { const states = {}; const setters = {}; initialstates.foreach(([key, initialvalue]) => { const [state, setstate] = usestate(initialvalue); states[key] = state; setters[key] = setstate; }); return [states, setters];};export default usedynamicstate;
// App.js (Using the Hooks Factory)import React from 'react';import useDynamicState from './useDynamicState';const App = () => { const [states, setters] = useDynamicState([ ['name', ''], ['age', 0], ]); return ( setters.name(e.target.value)} /> setters.age(parseInt(e.target.value))} /> Name: {states.name}
Age: {states.age}
);};export default App;
这个钩子工厂动态创建和管理多个状态,提供灵活性和更简洁的代码。
结论
通过利用这些设计模式,您可以创建更健壮、可扩展和可维护的 react 应用程序。这些模式可帮助您编写符合最佳实践的干净、可重用的代码,确保您的应用程序随着时间的推移更容易开发和管理。
您想更深入地研究这些模式或探索其他主题吗?
以上就是ReactJS 设计模式:编写健壮且可扩展的组件的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1491285.html
微信扫一扫
支付宝扫一扫