本篇内容介绍了“React代码的使用方法教程”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联建站服务项目包括万秀网站建设、万秀网站制作、万秀网页制作以及万秀网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,万秀网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到万秀省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
1. 仅对一个条件进行渲染
如果需要在条件为 true 时渲染某些内容,而在条件为 false 时不渲染任何内容,不要使 三元表达式,请改用 &&。
?‍♂️ 不推荐示例:
import React, { useState } from 'react' export const ConditionalRenderingWhenTrueBad = () => { const [showConditionalText, setShowConditionalText] = useState(false) const handleClick = () => setShowConditionalText(showConditionalText => !showConditionalText) return ( <div> <button onClick={handleClick}>Toggle the text</button> {/* 三元表达式 */} {showConditionalText ? <p>条件为 True!</p> : null} </div> ) }
? 推荐示例:
import React, { useState } from 'react' export const ConditionalRenderingWhenTrueGood = () => { const [showConditionalText, setShowConditionalText] = useState(false) const handleClick = () => setShowConditionalText(showConditionalText => !showConditionalText) return ( <div> <button onClick={handleClick}>Toggle the text</button> {showConditionalText && <p>条件为 True!</p>} </div> ) }
2. 每一个条件都进行渲染
如果需要在条件为 true 时渲染某些内容,而在条件为 false 时渲染其他内容。使用三元表达式!
?‍♂️ 不推荐的示例:
import React, { useState } from 'react' export const ConditionalRenderingBad = () => { const [showConditionOneText, setShowConditionOneText] = useState(false) const handleClick = () => setShowConditionOneText(showConditionOneText => !showConditionOneText) return ( <div> <button onClick={handleClick}>Toggle the text</button> {/* 条件 True 和 False 都要渲染内容 */} {showConditionOneText && <p>条件为 True!</p>} {!showConditionOneText && <p>条件为 Flase!</p>} </div> ) }
? 推荐示例:
import React, { useState } from 'react' export const ConditionalRenderingGood = () => { const [showConditionOneText, setShowConditionOneText] = useState(false) const handleClick = () => setShowConditionOneText(showConditionOneText => !showConditionOneText) return ( <div> <button onClick={handleClick}>Toggle the text</button> {showConditionOneText ? ( <p>The condition must be true!</p> ) : ( <p>The condition must be false!</p> )} </div> ) }
3. Boolean props
Props 值为 true 的推荐省略不写。
?‍♂️ 不推荐示例:
import React from 'react' const HungryMessage = ({ isHungry }) => ( <span>{isHungry ? 'I am hungry' : 'I am full'}</span> ) export const BooleanPropBad = () => ( <div> <span> <b>This person is hungry: </b> </span> <HungryMessage isHungry={true} /> <br /> <span> <b>This person is full: </b> </span> <HungryMessage isHungry={false} /> </div> )
? 推荐示例:
import React from 'react' const HungryMessage = ({ isHungry }) => ( <span>{isHungry ? 'I am hungry' : 'I am full'}</span> ) export const BooleanPropGood = () => ( <div> <span> <b>This person is hungry: </b> </span> {/* 不需要赋值 true,省略 */} <HungryMessage isHungry /> <br /> <span> <b>This person is full: </b> </span> <HungryMessage isHungry={false} /> </div> )
4. String props
Props 值为 String, 使用双引号,不使用花括号或反引号。
?‍♂️ 不推荐示例:
import React from 'react' const Greeting = ({ personName }) => <p>Hi, {personName}!</p> export const StringPropValuesBad = () => ( <div> <Greeting personName={"John"} /> <Greeting personName={'Matt'} /> <Greeting personName={`Paul`} /> </div> )
? 推荐示例:
import React from 'react' const Greeting = ({ personName }) => <p>Hi, {personName}!</p> export const StringPropValuesGood = () => ( <div> <Greeting personName="John" /> <Greeting personName="Matt" /> <Greeting personName="Paul" /> </div> )
5. Event handler functions
如果一个事件函数只接受一个参数,不需要传入匿名函数:onChange={e=>handleChange(e)},推荐这种写法:onChange={handleChange} 。
?‍♂️ 不推荐示例:
import React, { useState } from 'react' export const UnnecessaryAnonymousFunctionsBad = () => { const [inputValue, setInputValue] = useState('') const handleChange = e => { setInputValue(e.target.value) } return ( <> <label htmlFor="name">Name: </label> {/* 事件只有一个参数,不需要匿名函数*/} <input id="name" value={inputValue} onChange={e => handleChange(e)} /> </> ) }
? 推荐示例:
import React, { useState } from 'react' export const UnnecessaryAnonymousFunctionsGood = () => { const [inputValue, setInputValue] = useState('') const handleChange = e => { setInputValue(e.target.value) } return ( <> <label htmlFor="name">Name: </label> <input id="name" value={inputValue} onChange={handleChange} /> </> ) }
6. components as props
将组件作为参数传递给另一个组件时,如果该组件不接受任何参数,则无需将该传递的组件包装在函数中。
?‍♂️ 不推荐示例:
import React from 'react' const CircleIcon = () => ( <svg height="100" width="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ) const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( <div> <p>Below is the icon component prop I was given:</p> <IconComponent /> </div> ) export const UnnecessaryAnonymousFunctionComponentsBad = () => ( {/* 组件不需要包装在函数中 */} <ComponentThatAcceptsAnIcon IconComponent={() => <CircleIcon />} /> )
? 推荐示例:
import React from 'react' const CircleIcon = () => ( <svg height="100" width="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ) const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( <div> <p>Below is the icon component prop I was given:</p> <IconComponent /> </div> ) export const UnnecessaryAnonymousFunctionComponentsGood = () => ( <ComponentThatAcceptsAnIcon IconComponent={CircleIcon} /> )
7. undefined props
如果参数为 undefined 是允许的,那么不要提供 undefined 作为回退值。
?‍♂️ 不推荐示例:
import React from 'react' const ButtonOne = ({ handleClick }) => ( <button onClick={handleClick || undefined}>Click me</button> ) const ButtonTwo = ({ handleClick }) => { const noop = () => {} return <button onClick={handleClick || noop}>Click me</button> } export const UndefinedPropsBad = () => ( <div> <ButtonOne /> <ButtonOne handleClick={() => alert('Clicked!')} /> <ButtonTwo /> <ButtonTwo handleClick={() => alert('Clicked!')} /> </div> )
? 推荐示例:
import React from 'react' const ButtonOne = ({ handleClick }) => ( <button onClick={handleClick}>Click me</button> ) export const UndefinedPropsGood = () => ( <div> <ButtonOne /> <ButtonOne handleClick={() => alert('Clicked!')} /> </div> )
8. 设置 state 依赖先前的 state
如果新 state 依赖于先前 state,则始终将 state 设置为先前 state 的函数。可以批处理 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 ( <div> <button disabled={isDisabled}> I'm {isDisabled ? 'disabled' : 'enabled'} </button> <button onClick={toggleButton}>Toggle button state</button> <button onClick={toggleButton2Times}>Toggle button state 2 times</button> </div> ) }
? 推荐示例:
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 ( <div> <button disabled={isDisabled}> I'm {isDisabled ? 'disabled' : 'enabled'} </button> <button onClick={toggleButton}>Toggle button state</button> <button onClick={toggleButton2Times}>Toggle button state 2 times</button> </div> ) }
“React代码的使用方法教程”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
文章标题:React代码的使用方法教程
当前URL:https://www.cdcxhl.com/article40/pedeho.html
成都网站建设公司_创新互联,为您提供网站内链、响应式网站、搜索引擎优化、用户体验、营销型网站建设、ChatGPT
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联