React你应该学会的开发技巧

开发 前端
干净的代码不仅仅是工作代码。简洁的代码易于阅读,易于理解并且井井有条。在本文中,我们将研究六种编写更简洁的React代码的方法。

[[385445]]

干净的代码不仅仅是工作代码。简洁的代码易于阅读,易于理解并且井井有条。在本文中,我们将研究六种编写更简洁的React代码的方法。

在阅读这些建议时,请务必记住它们的实质:相信这些实践对我们编写自己的React代码很有帮助。让我们一起学习吧!

1.仅针对一种条件渲染

如果你要为某个条件成立时渲染某些元素,请不要使用三元运算符。请改用&&运算符。

不推荐写法:

  1. import React, { useState } from 'react' 
  2. export const ConditionalRenderingWhenTrueBad = () => { 
  3.   const [showConditionalText, setShowConditionalText] = useState(false
  4.   const handleClick = () => 
  5.     setShowConditionalText(showConditionalText => !showConditionalText) 
  6.      
  7.   return ( 
  8.     <div> 
  9.       <button onClick={handleClick}>切换文本</button> 
  10.       {showConditionalText ? <p>成立显示内容</p> : null
  11.     </div> 
  12.   ) 

推荐写法:

  1. import React, { useState } from 'react' 
  2. export const ConditionalRenderingWhenTrueGood = () => { 
  3.   const [showConditionalText, setShowConditionalText] = useState(false
  4.  
  5.   const handleClick = () => 
  6.     setShowConditionalText(showConditionalText => !showConditionalText) 
  7.  
  8.   return ( 
  9.     <div> 
  10.       <button onClick={handleClick}>切换文本</button> 
  11.       {showConditionalText && <p>成立显示内容!</p>} 
  12.     </div> 
  13.   ) 

2.Boolean Props简写

isHungry处简写了

不推荐写法:

  1. import React from 'react' 
  2. const HungryMessage = ({ isHungry }) => ( 
  3.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  4.  
  5. export const BooleanPropBad = () => ( 
  6.   <div> 
  7.     <HungryMessage isHungry={true} /> 
  8.     <HungryMessage isHungry={false} /> 
  9.   </div> 

推荐写法:

  1. import React from 'react' 
  2. const HungryMessage = ({ isHungry }) => ( 
  3.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  4.  
  5. export const BooleanPropGood = () => ( 
  6.   <div> 
  7.     <HungryMessage isHungry /> 
  8.     <HungryMessage isHungry={false} /> 
  9.   </div> 

3.String Props简写

personName处简写了

不推荐写法:

  1. import React from 'react' 
  2. const Greeting = ({ personName }) => <p>Hi, {personName}!</p> 
  3.  
  4. export const StringPropValuesBad = () => ( 
  5.   <div> 
  6.     <Greeting personName={"John"} /> 
  7.     <Greeting personName={'Matt'} /> 
  8.     <Greeting personName={`Paul`} /> 
  9.   </div> 

推荐写法:

  1. import React from 'react' 
  2. const Greeting = ({ personName }) => <p>Hi, {personName}!</p> 
  3.  
  4. export const StringPropValuesGood = () => ( 
  5.   <div> 
  6.     <Greeting personName="John" /> 
  7.     <Greeting personName="Matt" /> 
  8.     <Greeting personName="Paul" /> 
  9.   </div> 

4.事件处理函数简写

onChange处简写了

不推荐写法:

  1. import React, { useState } from 'react' 
  2. export const UnnecessaryAnonymousFunctionsBad = () => { 
  3.   const [inputValue, setInputValue] = useState(''
  4.  
  5.   const handleChange = e => { 
  6.     setInputValue(e.target.value) 
  7.   } 
  8.  
  9.   return ( 
  10.     <> 
  11.       <label htmlFor="name">Name: </label> 
  12.       <input id="name" value={inputValue} onChange={e => handleChange(e)} /> 
  13.     </> 
  14.   ) 

推荐写法:

  1. import React, { useState } from 'react' 
  2. export const UnnecessaryAnonymousFunctionsGood = () => { 
  3.   const [inputValue, setInputValue] = useState(''
  4.   const handleChange = e => { 
  5.     setInputValue(e.target.value) 
  6.   } 
  7.  
  8.   return ( 
  9.     <> 
  10.       <label htmlFor="name">Name: </label> 
  11.       <input id="name" value={inputValue} onChange={handleChange} /> 
  12.     </> 
  13.   ) 

5.组件作为参数返回

IconComponent处简写了

不推荐写法:

  1. import React from 'react' 
  2. const CircleIcon = () => ( 
  3.   <svg height="100" width="100"
  4.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  5.   </svg> 
  6.  
  7. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  8.   <div> 
  9.     <IconComponent /> 
  10.   </div> 
  11.  
  12. export const UnnecessaryAnonymousFunctionComponentsBad = () => ( 
  13.   <ComponentThatAcceptsAnIcon IconComponent={() => <CircleIcon />} /> 

推荐写法:

  1. import React from 'react' 
  2. const CircleIcon = () => ( 
  3.   <svg height="100" width="100"
  4.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  5.   </svg> 
  6.  
  7. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  8.   <div> 
  9.     <IconComponent /> 
  10.   </div> 
  11.  
  12. export const UnnecessaryAnonymousFunctionComponentsGood = () => ( 
  13.   <ComponentThatAcceptsAnIcon IconComponent={CircleIcon} /> 

6.设置依赖于先前pros的pros

如果新状态依赖于先前状态,则始终将状态设置为先前状态的函数。可以批处理React状态更新,并且不以这种方式编写更新会导致意外结果,setIsDisabled处简写

不推荐写法:

  1. import React, { useState } from 'react' 
  2. export const PreviousStateBad = () => { 
  3.   const [isDisabled, setIsDisabled] = useState(false
  4.   const toggleButton = () => setIsDisabled(!isDisabled) 
  5.  
  6.   const toggleButton2Times = () => { 
  7.     for (let i = 0; i < 2; i++) { 
  8.       toggleButton() 
  9.     } 
  10.   } 
  11.  
  12.   return ( 
  13.     <div> 
  14.       <button disabled={isDisabled}> 
  15.         I'm {isDisabled ? 'disabled' : 'enabled'} 
  16.       </button> 
  17.       <button onClick={toggleButton}>切换按钮状态</button> 
  18.       <button onClick={toggleButton2Times}>切换按钮状态2次</button> 
  19.     </div> 
  20.   ) 

推荐写法:

  1. import React, { useState } from 'react' 
  2. export const PreviousStateGood = () => { 
  3.   const [isDisabled, setIsDisabled] = useState(false
  4.   const toggleButton = () => setIsDisabled(isDisabled => !isDisabled) 
  5.  
  6.   const toggleButton2Times = () => { 
  7.     for (let i = 0; i < 2; i++) { 
  8.       toggleButton() 
  9.     } 
  10.   } 
  11.  
  12.   return ( 
  13.     <div> 
  14.       <button disabled={isDisabled}> 
  15.         I'm {isDisabled ? 'disabled' : 'enabled'} 
  16.       </button> 
  17.       <button onClick={toggleButton}>切换按钮状态</button> 
  18.       <button onClick={toggleButton2Times}>切换按钮状态2次</button> 
  19.     </div> 
  20.   ) 

本文转载自微信公众号「前端人」,可以通过以下二维码关注。转载本文请联系前端人公众号。

 

责任编辑:武晓燕 来源: 前端人
相关推荐

2023-01-03 09:00:52

React前端

2021-10-09 10:50:30

JavaScript编程开发

2011-03-25 15:56:58

2013-01-09 13:55:43

2022-07-18 08:08:16

Go​语言技巧

2020-02-21 10:30:10

开发技能代码

2020-06-02 10:10:46

React前端组件

2021-10-25 14:55:38

Linux技巧命令

2022-10-24 00:44:32

IO远程操作数据库

2022-06-29 10:06:27

Webpack优化技巧前端

2014-03-04 09:35:45

JavaScript调试

2022-11-16 09:04:36

SQL查询SELECT

2023-12-07 07:03:09

2020-04-03 19:21:59

JavaScript编程语言开发

2021-04-12 15:54:45

Android 开发技巧

2015-05-07 10:23:19

Android学习资源

2023-08-22 10:25:19

CSS动画网页

2024-01-30 08:30:41

TypeScript编译器类型

2010-11-09 10:03:26

2021-06-26 10:04:23

Code特性技巧
点赞
收藏

51CTO技术栈公众号