简体   繁体   English

决定何时使用句柄函数以及何时封装在 React 中的按钮内

[英]Deciding when to use handle functions and when to encapsulate inside a button in React

I'm a beginner in programming and I'm trying to understand when it's a good idea to use "handle functions" in my code and when it's better to keep things simple and just put everything inside the button, for example.我是编程初学者,我想了解什么时候在我的代码中使用“句柄函数”是个好主意,什么时候最好保持简单,例如将所有内容都放在按钮中。 Can someone help me understand the differences and when it is appropriate to use each approach?有人可以帮助我了解差异以及何时适合使用每种方法吗?

I would consider this choice to be about readability of code, conventions of the codebase, and personal preference.我认为这种选择与代码的可读性、代码库的约定和个人偏好有关。 If it's more than a few lines, I would separate the function from the button to not bloat the JSX.如果超过几行,我会将 function 与按钮分开,以免使 JSX 膨胀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 React 中的用户点击“后退按钮”时,处理在 useEffect 中重新调用 function - Handle re-calling a function inside useEffect when "Back Button" clicked from a user in React React:何时将函数用作子代以及何时使用组件 - React: When to use functions as children and when to use components 按下单元格内部的按钮时处理单击事件 - Handle click event when button inside cell is pressed 在 React 应用程序中使用 iframe 时句柄拒绝连接错误 - Handle refused to connect error when using iframe inside react app 在不同函数中更新特定变量时如何使用回调 - How to use callbacks when specific variable is updated inside different functions 调用 javascript 函数时使用反应钩子的正确方法是什么? - What is proper way to use react hooks when calling javascript functions? 如何添加一个<field />当使用 React 和 Formik 在表单中单击按钮时? - How to add a <Field /> when a button is clicked inside a Form with React & Formik? 使用嵌套在 Link 标签内的按钮时反应表单不提交 - React form not submitting when using button nested inside Link tag 重新加载/刷新时,封装URL以修复React js应用程序页面上的404错误 - Encapsulate URL to fix 404 error on react js application page when we reload/refresh 在反应中找不到产品时处理 - handle when product not found in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM