第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React:Hooks 會(huì)取代 HOC 和渲染道具嗎?

React:Hooks 會(huì)取代 HOC 和渲染道具嗎?

手掌心 2021-06-08 13:14:38
從React Hooks FAQ 中,我們了解到鉤子可以替代返回/渲染單個(gè)組件的 HOC 和渲染道具。我試圖更好地理解這一點(diǎn),以及為什么這是事實(shí)。我們先來看看 HOC:HOC 是一個(gè)函數(shù),它將一個(gè)組件作為參數(shù),將它包裝在周圍的邏輯中,如效果和狀態(tài),并返回一個(gè)新組件。自定義鉤子究竟如何取代它?我們?nèi)匀恍枰闷渌壿嫲b輸入函數(shù)的函數(shù)。查看渲染道具:渲染道具是我們作為道具傳遞給另一個(gè)組件的組件,然后使用一些新的道具渲染傳遞的組件。我想我們可以通過創(chuàng)建一個(gè)返回完整組件的自定義鉤子來替換它,然后在任何需要的組件中使用該鉤子。因此,父級(jí)不必將組件作為道具傳遞給它的子級(jí)。這就是鉤子會(huì)取代渲染道具的方式嗎?對鉤子如何在最常見的用例中替換 HOC 和渲染道具的解釋,最好是代碼示例,將不勝感激。
查看完整描述

1 回答

?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊

HOC 和渲染道具有很多不同的用途,所以我不可能將它們?nèi)慷己w在內(nèi),但基本上該段落指出,您使用 HOC/渲染道具的許多情況也可以通過鉤子實(shí)現(xiàn)。這不會(huì)使 HOCs/render props 過時(shí),但鉤子是另一個(gè)可供您在組件之間共享代碼的工具。


HOC/render prop 的一項(xiàng)常見工作是管理一些數(shù)據(jù)的生命周期,并將該數(shù)據(jù)傳遞給派生組件或子組件。在以下示例中,目標(biāo)是獲取窗口寬度,包括與之相關(guān)的狀態(tài)管理和事件偵聽。


HOC 版本:


function withWindowWidth(BaseComponent) {

  class DerivedClass extends React.Component {

    state = {

      windowWidth: window.innerWidth,

    }


    onResize = () => {

      this.setState({

        windowWidth: window.innerWidth,

      })

    }


    componentDidMount() {

      window.addEventListener('resize', this.onResize)

    }


    componentWillUnmount() {

      window.removeEventListener('resize', this.onResize);

    }


    render() {

      return <BaseComponent {...this.props} {...this.state}/>

    }

  }

  // Extra bits like hoisting statics omitted for brevity

  return DerivedClass;

}


// To be used like this in some other file:


const MyComponent = (props) => {

  return <div>Window width is: {props.windowWidth}</div>

};


export default withWindowWidth(MyComponent);


渲染道具版本:


class WindowWidth extends React.Component {

  propTypes = {

    children: PropTypes.func.isRequired

  }


  state = {

    windowWidth: window.innerWidth,

  }


  onResize = () => {

    this.setState({

      windowWidth: window.innerWidth,

    })

  }


  componentDidMount() {

    window.addEventListener('resize', this.onResize)

  }


  componentWillUnmount() {

    window.removeEventListener('resize', this.onResize);

  }


  render() {

    return this.props.children(this.state.windowWidth);

  }

}


// To be used like this:


const MyComponent = () => {

  return (

    <WindowWidth>

      {width => <div>Window width is: {width}</div>}

    </WindowWidth>

  )

}

最后但并非最不重要的是,鉤子版本


const useWindowWidth = () => {

  const [width, setWidth] = useState(window.innerWidth);

  useEffect(() => {

    const onResize = () => setWidth(window.innerWidth);

    window.addEventListener('resize', onResize);

    return () => window.removeEventListener('resize', onResize);

  }, [])

  return width;

}


// To be used like this:


const MyComponent = () => {

  const width = useWindowWidth();

  return <div>Window width is: {width}</div>;

}


查看完整回答
反對 回復(fù) 2021-06-18
  • 1 回答
  • 0 關(guān)注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)