[[385445]]
创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站建设、宜良网络推广、成都微信小程序、宜良网络营销、宜良企业策划、宜良品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供宜良建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
干净的代码不仅仅是工作代码。简洁的代码易于阅读,易于理解并且井井有条。在本文中,我们将研究六种编写更简洁的React代码的方法。
在阅读这些建议时,请务必记住它们的实质:相信这些实践对我们编写自己的React代码很有帮助。让我们一起学习吧!
1.仅针对一种条件渲染
如果你要为某个条件成立时渲染某些元素,请不要使用三元运算符。请改用&&运算符。
不推荐写法:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueBad = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {showConditionalText ?
成立显示内容
: null}- )
- }
推荐写法:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueGood = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {showConditionalText &&
成立显示内容!
}- )
- }
2.Boolean Props简写
isHungry处简写了
不推荐写法:
- import React from 'react'
- const HungryMessage = ({ isHungry }) => (
- {isHungry ? 'I am hungry' : 'I am full'}
- )
- export const BooleanPropBad = () => (
- )
推荐写法:
- import React from 'react'
- const HungryMessage = ({ isHungry }) => (
- {isHungry ? 'I am hungry' : 'I am full'}
- )
- export const BooleanPropGood = () => (
- )
3.String Props简写
personName处简写了
不推荐写法:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesBad = () => (
- )
推荐写法:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesGood = () => (
- )
4.事件处理函数简写
onChange处简写了
不推荐写法:
- 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 (
- <>
- >
- )
- }
5.组件作为参数返回
IconComponent处简写了
不推荐写法:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
- )
- export const UnnecessaryAnonymousFunctionComponentsBad = () => (
} /> - )
推荐写法:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
- )
- export const UnnecessaryAnonymousFunctionComponentsGood = () => (
- )
6.设置依赖于先前pros的pros
如果新状态依赖于先前状态,则始终将状态设置为先前状态的函数。可以批处理React状态更新,并且不以这种方式编写更新会导致意外结果,setIsDisabled处简写
不推荐写法:
- 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你应该学会的开发技巧
链接地址:http://www.csdahua.cn/qtweb/news11/539511.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网