干净的代码易于阅读,简单易懂,而且组织整齐。在这篇文章中,列举了一些平时可能需要关注的点。

成都创新互联公司网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、响应式网站、行业门户型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,成都创新互联公司网站制作为客户规划、定制制作符合企业需求、带有营销价值的网络建站方案认真对待每一个客户,我们不用口头的语言来吹擂我们的优秀,上1000+的成功案例见证着我们的成长。
如果你不同意其中任何一条,那也完全没问题。
如果你需要在一个条件为真时有条件地呈现一些东西,在一个条件为假时不呈现任何东西,不要使用三元运算符。使用&&运算符代替。
- import React, { useState } from 'react'
 - export const ConditionalRenderingWhenTrueBad = () => {
 - const [showConditionalText, setShowConditionalText] = useState(false)
 - const handleClick = () =>
 - setShowConditionalText(showConditionalText => !showConditionalText)
 - return (
 - {showConditionalText ?
 The condition must be true!
: null}- )
 - }
 
- import React, { useState } from 'react'
 - export const ConditionalRenderingWhenTrueGood = () => {
 - const [showConditionalText, setShowConditionalText] = useState(false)
 - const handleClick = () =>
 - setShowConditionalText(showConditionalText => !showConditionalText)
 - return (
 - {showConditionalText &&
 The condition must be true!
}- )
 - }
 
如果你需要在一个条件为真时有条件地呈现一个东西,在条件为假时呈现另一个东西,请使用三元运算符。
- import React, { useState } from 'react'
 - export const ConditionalRenderingBad = () => {
 - const [showConditionOneText, setShowConditionOneText] = useState(false)
 - const handleClick = () =>
 - setShowConditionOneText(showConditionOneText => !showConditionOneText)
 - return (
 - {showConditionOneText &&
 The condition must be true!
}- {!showConditionOneText &&
 The condition must be false!
}- )
 - }
 
- import React, { useState } from 'react'
 - export const ConditionalRenderingGood = () => {
 - const [showConditionOneText, setShowConditionOneText] = useState(false)
 - const handleClick = () =>
 - setShowConditionOneText(showConditionOneText => !showConditionOneText)
 - return (
 - {showConditionOneText ? (
 The condition must be true!
- ) : (
 The condition must be false!
- )}
 - )
 - }
 
一个真实的props可以提供给一个组件,只有props名称而没有值,比如:myTruthyProp。写成myTruthyProp={true}是不必要的。
- import React from 'react'
 - const HungryMessage = ({ isHungry }) => (
 - {isHungry ? 'I am hungry' : 'I am full'}
 - )
 - export const BooleanPropBad = () => (
 - This person is hungry:
 
- This person is full:
 - )
 
- import React from 'react'
 - const HungryMessage = ({ isHungry }) => (
 - {isHungry ? 'I am hungry' : 'I am full'}
 - )
 - export const BooleanPropGood = () => (
 - This person is hungry:
 
- This person is full:
 - )
 
可以用双引号提供一个字符串道具值,而不使用大括号或反斜线。
- import React from 'react'
 - const Greeting = ({ personName }) =>
 Hi, {personName}!
- export const StringPropValuesBad = () => (
 - )
 
- import React from 'react'
 - const Greeting = ({ personName }) =>
 Hi, {personName}!
- export const StringPropValuesGood = () => (
 - )
 
如果一个事件处理程序只需要事件对象的一个参数,你就可以像这样提供函数作为事件处理程序:onChange={handleChange}。
你不需要像这样把函数包在一个匿名函数中。
- import React, { useState } from 'react'
 - export const UnnecessaryAnonymousFunctionsBad = () => {
 - const [inputValue, setInputValue] = useState('')
 - const handleChange = e => {
 - setInputValue(e.target.value)
 - }
 - return (
 - <>
 - handleChange(e)} />
 - >
 - )
 - }
 
- import React, { useState } from 'react'
 - export const UnnecessaryAnonymousFunctionsGood = () => {
 - const [inputValue, setInputValue] = useState('')
 - const handleChange = e => {
 - setInputValue(e.target.value)
 - }
 - return (
 - <>
 - >
 - )
 - }
 
当把一个组件作为props传递给另一个组件时,如果该组件不接受任何props,你就不需要把这个传递的组件包裹在一个函数中。
- import React from 'react'
 - const CircleIcon = () => (
 - )
 - const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
 Below is the icon component prop I was given:
- )
 - export const UnnecessaryAnonymousFunctionComponentsBad = () => (
 } /> - )
 
- import React from 'react'
 - const CircleIcon = () => (
 - )
 - const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
 Below is the icon component prop I was given:
- )
 - export const UnnecessaryAnonymousFunctionComponentsGood = () => (
 - )
 
未定义的props被排除在外,所以如果props未定义是可以的,就不要担心提供未定义的回退。
- import React from 'react'
 - const ButtonOne = ({ handleClick }) => (
 - )
 - const ButtonTwo = ({ handleClick }) => {
 - const noop = () => {}
 - return
 - }
 - export const UndefinedPropsBad = () => (
 alert('Clicked!')} /> alert('Clicked!')} /> - )
 
- import React from 'react'
 - const ButtonOne = ({ handleClick }) => (
 - )
 - export const UndefinedPropsGood = () => (
 alert('Clicked!')} /> - )
 
如果新的状态依赖于之前的状态,那么一定要把状态设置为之前状态的函数。React的状态更新可以是分批进行的,如果不这样写你的更新就会导致意外的结果。
- import React, { useState } from 'react'
 - export const PreviousStateBad = () => {
 - const [isDisabled, setIsDisabled] = useState(false)
 - const toggleButton = () => setIsDisabled(!isDisabled)
 - const toggleButton2Times = () => {
 - for (let i = 0; i < 2; i++) {
 - toggleButton()
 - }
 - }
 - return (
 - I'm {isDisabled ? 'disabled' : 'enabled'}
 - )
 - }
 
- import React, { useState } from 'react'
 - export const PreviousStateGood = () => {
 - const [isDisabled, setIsDisabled] = useState(false)
 - const toggleButton = () => setIsDisabled(isDisabled => !isDisabled)
 - const toggleButton2Times = () => {
 - for (let i = 0; i < 2; i++) {
 - toggleButton()
 - }
 - }
 - return (
 - I'm {isDisabled ? 'disabled' : 'enabled'}
 - )
 - }
 
以下做法并非针对React,而是在JavaScript(以及任何编程语言)中编写干净代码的良好做法。
稍微做个总结:
我是TianTian,我们下一期见!!!
                分享标题:编写简洁的React代码建议
                
                标题URL:http://www.csdahua.cn/qtweb/news24/402424.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网