国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

超級(jí)工程網(wǎng)站建設(shè)網(wǎng)站優(yōu)化排名的方法

超級(jí)工程網(wǎng)站建設(shè),網(wǎng)站優(yōu)化排名的方法,office網(wǎng)站開發(fā),夢(mèng)織和wordpress特點(diǎn)文章目錄render-props 模式props 方式children 方式(推薦)Hoc(高階組件)使用步驟示例props 丟失解決方案自定義 hook1.只執(zhí)行一次2.防抖hook高階組件與自定義hook有什么區(qū)別相同點(diǎn)不同點(diǎn)React 中代碼邏輯復(fù)用有三種方式,render-props, Hoc&am…

React 中代碼邏輯復(fù)用有三種方式,render-props, Hoc,·自定義hooks·

注意: render-props, Hoc這兩種方式不是新的API,而是利用React自身特點(diǎn)的編碼技巧,演化而成的固定模式(寫法)

render-props 模式

注意: 并不是該模式叫 render props 就必須使用名為 render 的 prop,實(shí)際上可以使用任意名稱的 prop

props 方式

封裝一個(gè) render-props 模式下的鼠標(biāo)移動(dòng),得到鼠標(biāo)當(dāng)前移動(dòng)位置

// 封裝的組件
class Mounse extends React.Component {state = {x: 0,y: 0}componentDidMount(){window.addEventListener('mousemove', this.mouseMove)}componentWillUnmount(){window.removeEventListener("mousemove", this.mouseMove)}mouseMove= (e) => {this.setState({x: e.clientX,y: e.clientY}) } render(){renten this.props.render(this.state)}
}// 使用
export default function Index() {return (<h1><Mouns render={(mouse)=>{return <p>x: { mouse.x }----y: { mouse.y }</p>}} />  )
}

children 方式(推薦)

// 封裝的組件
class Mounse extends React.Component {state = {x: 0,y: 0}componentDidMount(){window.addEventListener('mousemove', this.mouseMove)}componentWillUnmount(){window.removeEventListener("mousemove", this.mouseMove)}mouseMove= (e) => {this.setState({x: e.clientX,y: e.clientY}) } render(){renten this.props.children(this.state)}
}

使用

// 使用
export default function Index() {return (<h1><Mouns>{(mouse)=>{<p>x: { mouse.x }----y: { mouse.y }</p>}}</Mouns>  )
}

Hoc(高階組件)

高階組件使用一個(gè)函數(shù),接收要包裝的組件,返回一個(gè)增強(qiáng)后的組件

使用步驟

  1. 創(chuàng)建一個(gè)函數(shù),以 with 開頭
  2. 指定函數(shù)參數(shù),函數(shù)參數(shù)為一個(gè)組件,組件以大寫字母開頭
  3. 在函數(shù)內(nèi)創(chuàng)建一個(gè)類組件,提供狀態(tài)邏輯代碼,并返回

示例

function WithMounse(Com) {class Mounse extends PureComponent {state = {x: 0,y: 0}componentDidMount(){window.addEventListener('mousemove',this.handleMonve)}componentWillUnmount(){window.removeEventListener("mousemove", this.handleMonve)}handleMonve = e => { this.setState({x: e.clientX,y: e.clientY})}render(){return <Com {...this.state} />}}return <Mounse  />}const Foo = props => { return <p>{props.x}...{props.y}</p>}const EndCom = WithMounse(Foo).type// 調(diào)用class App extends PureComponent {render() {return (<EndCom />)}}

props 丟失

問題示范

由圖片可以看出,在高階組件中傳入一個(gè) props 屬性 a = 1 在組件里面接收不到,這就是屬性丟失

解決方案

在高階組件封裝的時(shí),對(duì)props屬性再次進(jìn)行傳遞

示例

 <Com {...this.state} {...this.props}  />
function WithMounse(Com) {class Mounse extends PureComponent {state = {x: 0,y: 0}componentDidMount(){window.addEventListener('mousemove',this.handleMonve)}componentWillUnmount(){window.removeEventListener("mousemove", this.handleMonve)}handleMonve = e => { this.setState({x: e.clientX,y: e.clientY})}render(){return <Com {...this.state} {...this.props}  />}}return <Mounse  />}

自定義 hook

hook 是react16.8 的新特性,它可以在你不編寫class組件的情況下使用state一級(jí)其他的React的特性

通過自定義hook,可以將組件邏輯提取到可重復(fù)的函數(shù)中

注意:自定義hook 一定以 use 開頭,例如 useDebonce,useQuery等等

下面是幾個(gè)自定義 hook 的封裝

1.只執(zhí)行一次

export const useMount = (callback: () => void) => {useEffect(() => {callback()}, [])
}

使用

  useMount(()=>{// 數(shù)據(jù)請(qǐng)求})

2.防抖hook

export const useDebonce = <T>(value: T, delay?: number): T => {const [debounce, setDebounce] = useState(value)useEffect(()=>{let timer = setTimeout(()=>{setDebounce(value)}, delay)return ()=> clearTimeout(timer) },[value, delay])return debounce
}

使用

 const changeValue = '改變所依賴的值'const Debonce = useDebonce(changeValue, 300)useEffect(()=>{console.log('changeValue')},[Debonce])

高階組件與自定義hook有什么區(qū)別

相同點(diǎn)

  • 都是對(duì)組件邏輯的封裝,達(dá)到組件邏輯復(fù)用的目的

不同點(diǎn)

  • 定義方式不同:高階組件以with開頭,自定義hook以 use開頭
  • 特性不同:高階組件是類組件中的總結(jié)出來的一種編碼技巧,自定義hook 是 react16.8 后出來的新特性
  • 使用場(chǎng)景不同: 高階組件一般在函數(shù)組件中使用,自定義hook只有函數(shù)組件中有
  • 返回值不同:高階組件的返回值是一個(gè)組件,自定義hook的返回值可有可無
http://aloenet.com.cn/news/43786.html

相關(guān)文章:

  • 織夢(mèng)后臺(tái)怎么加自己做的網(wǎng)站長(zhǎng)春seo快速排名
  • 開發(fā)公司起名seo網(wǎng)站快速排名
  • 微網(wǎng)站和小程序的區(qū)別站長(zhǎng)統(tǒng)計(jì)app下載大全
  • 彩票娛樂網(wǎng)站建設(shè)開發(fā)百度競(jìng)價(jià)排名正確解釋
  • 做信息網(wǎng)站怎么賺錢網(wǎng)絡(luò)營(yíng)銷廣告策劃
  • 杭州灣新區(qū)建設(shè)局網(wǎng)站營(yíng)銷咨詢師
  • 解決做網(wǎng)站問題上海最新新聞
  • 網(wǎng)站bbs備案龍崗網(wǎng)站設(shè)計(jì)
  • 做網(wǎng)站備案湯陰縣seo快速排名有哪家好
  • 財(cái)務(wù)咨詢網(wǎng)站模板長(zhǎng)沙縣網(wǎng)絡(luò)營(yíng)銷咨詢
  • 個(gè)人網(wǎng)站做淘寶客犯法嗎寫軟文怎么接單子
  • 網(wǎng)站開發(fā)的主要特點(diǎn)網(wǎng)絡(luò)推廣公司網(wǎng)站
  • 汕頭住房與城鄉(xiāng)建設(shè)網(wǎng)站實(shí)體店引流推廣方法
  • 阿米納網(wǎng)站建設(shè)網(wǎng)上互聯(lián)網(wǎng)推廣
  • 做淘寶聯(lián)盟網(wǎng)站要多少錢百度高級(jí)搜索技巧
  • 阿里巴巴網(wǎng)站分類板塊做全屏全網(wǎng)營(yíng)銷推廣方案
  • 成都房地產(chǎn)最新政策seo是哪個(gè)英文的縮寫
  • 做外貿(mào)網(wǎng)站選美國(guó)服務(wù)器的費(fèi)用百度愛采購(gòu)優(yōu)化
  • 電子商務(wù)網(wǎng)站建設(shè)與實(shí)踐上機(jī)指導(dǎo)教程網(wǎng)絡(luò)營(yíng)銷最火的案例
  • 網(wǎng)站建站發(fā)布平臺(tái)企業(yè)網(wǎng)站建設(shè)推廣
  • 網(wǎng)站建設(shè)和維護(hù)公司百度怎么收錄自己的網(wǎng)站
  • window2003iis建好的網(wǎng)站上海網(wǎng)站排名優(yōu)化怎么做
  • 鎮(zhèn)海官方網(wǎng)站建設(shè)網(wǎng)絡(luò)銷售怎么做才能有業(yè)務(wù)
  • 做網(wǎng)站北京培訓(xùn)網(wǎng)絡(luò)營(yíng)銷的機(jī)構(gòu)
  • 裝修廣告做哪個(gè)網(wǎng)站最好看百度站長(zhǎng)平臺(tái)網(wǎng)頁(yè)版
  • 鹽城企業(yè)做網(wǎng)站多少錢網(wǎng)絡(luò)營(yíng)銷研究背景及意義
  • 國(guó)內(nèi)知名互聯(lián)網(wǎng)公司泉州seo按天計(jì)費(fèi)
  • 專業(yè)做展會(huì)網(wǎng)站網(wǎng)絡(luò)營(yíng)銷成功的品牌
  • 新鄉(xiāng)網(wǎng)站制作手機(jī)怎么做網(wǎng)站免費(fèi)的
  • WordPress短碼生成器廣州seo顧問seocnm