最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在React中強(qiáng)制重新渲染的4 種方式案例代碼

 更新時(shí)間:2023年12月20日 15:28:01   作者:大海里的一條魚.  
這篇文章主要介紹了在React中強(qiáng)制重新渲染的4 種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

1.在 state 改變時(shí)重新渲染組件

React 組件在每次 state 變化時(shí)都會(huì)運(yùn)行 render() 方法。

class App extends React.Component {
  componentDidMount() {
    this.setState({});
  }
  render() {
    console.log('render() method')
    return <h1>Hi!</h1>;
  }
}

在上面的例子中,在組件掛載完成之后更新了 state。

也可以在事件監(jiān)聽器中觸發(fā)重新渲染組件,例如 click 事件里。

class App extends React.Component {
  state = {
    mssg: ""
  };
  handleClick = () => {
    this.setState({ mssg: "Hi there!" });
  };
  render() {
    console.log("render() method");
    return (
      <>
        <button onClick={this.handleClick}>Say something</button>
        <div>{this.state.mssg}</div>
      </>
    );
  }
}

以上都會(huì)輸出如下:

render() method 
render() method

2.在 props 改變時(shí)重新渲染組件

class Child extends React.Component {
  render() {
    console.log('Child component: render()');
    return this.props.message;
  }
}
class App extends React.Component {
  state = {
    mssg: ""
  };
  handleClick = () => {
    this.setState({ mssg: "Hi there!" });
  };
  render() {
    return (
      <>
        <button onClick={this.handleClick}>Say something</button>
        <Child message={this.state.mssg} />
      </>
    );
  }
}

上述例子中 <Child /> 組件不含有 state,但它接收了一個(gè) prop 名為 message。

點(diǎn)擊按鈕后,會(huì)更新 <Child /> 組件,會(huì)引起 render() 再次執(zhí)行。

Child component: render() 
Child component: render()

3.借助 key prop 重新渲染

上述更新 state 和 props 的操作不會(huì)引起組件的重新掛載/卸載,只會(huì)重新調(diào)用 render() 方法。有時(shí)候?qū)τ谝恍┻壿嫃?fù)雜的組件,我們需要重新掛載/卸載操作,以便重新渲染內(nèi)容。

class Child extends React.Component {
  componentWillUnmount() {
    console.log("will unmount");
  }
  render() {
    console.log("Child component: render()");
    return this.props.message;
  }
}
class App extends React.Component {
  state = {
    messages: [
      { id: 1, title: "Hello from Beijing", content: "Welcome to Beijing" },
      { id: 2, title: "Hello from London", content: "Welcome to London" },
      { id: 3, title: "Hello from Tokyo", content: "Welcome to Tokyo" }
    ],
    activeId: null
  };
  render() {
    const { messages, activeId } = this.state;
    return (
      <>
        <ul>
          {messages.map((item) => (
            <li
              key={item.id}
              onClick={() => {
                this.setState({ activeId: item.id });
              }}
            >
              {item.title}
            </li>
          ))}
        </ul>
        <Child
          key={activeId}
          message={
            activeId
              ? messages.find((item) => item.id === activeId).content
              : ""
          }
        />
      </>
    );
  }
}

上述的這個(gè)例子,當(dāng)用戶點(diǎn)擊標(biāo)題時(shí),我們想要重新掛載/卸載整個(gè)子組件,這時(shí)可以在子組件上增加一個(gè) key 屬性,這樣便實(shí)現(xiàn)了目的??梢钥吹矫看吸c(diǎn)擊后,都會(huì)執(zhí)行 componentWillUnmount() 方法。

4.強(qiáng)制重新渲染

不建議采用此方式,建議采用更新 props 和 state 的方式。

class App extends React.Component {
  handleClick = () => {
    // force a re-render
    this.forceUpdate();
  };
  render() {
    console.log('App component: render()')
    return (
      <>
        <button onClick={this.handleClick}>Say something</button>
      </>
    );
  }
}

到此這篇關(guān)于在 React 中強(qiáng)制重新渲染的 4 種方式的文章就介紹到這了,更多相關(guān)React 重新渲染內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React中props驗(yàn)證不足的問題及解決方案

    React中props驗(yàn)證不足的問題及解決方案

    在?React?開發(fā)中,props?是組件間通信的重要方式,通過?props,父組件可以向子組件傳遞數(shù)據(jù)和回調(diào)函數(shù),然而,如果對(duì)?props?的驗(yàn)證不足,可能會(huì)導(dǎo)致類型錯(cuò)誤、運(yùn)行時(shí)錯(cuò)誤或難以調(diào)試的問題,本文將探討?React?中?props?驗(yàn)證不足的常見問題,并提供解決方案
    2025-03-03
  • 采用React編寫小程序的Remax框架的編譯流程解析(推薦)

    采用React編寫小程序的Remax框架的編譯流程解析(推薦)

    這篇文章主要介紹了采用React編寫小程序的Remax框架的編譯流程解析(推薦),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • react-navigation之動(dòng)態(tài)修改title的內(nèi)容

    react-navigation之動(dòng)態(tài)修改title的內(nèi)容

    這篇文章主要介紹了react-navigation之動(dòng)態(tài)修改title的內(nèi)容,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • 使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑

    使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑

    相比于react-dnd,react-beautiful-dnd更適用于列表之間拖拽的場(chǎng)景,本文主要介紹了使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑,感興趣的可以了解一下
    2021-05-05
  • Iconfont不能上傳如何維護(hù)Icon

    Iconfont不能上傳如何維護(hù)Icon

    這篇文章主要為大家介紹了在Iconfont還是不能上傳,要如何維護(hù)你的Icon,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • React中關(guān)于render()的用法及說明

    React中關(guān)于render()的用法及說明

    這篇文章主要介紹了React中關(guān)于render()的用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • react native仿微信PopupWindow效果的實(shí)例代碼

    react native仿微信PopupWindow效果的實(shí)例代碼

    本篇文章主要介紹了react native仿微信PopupWindow效果的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • antd之RangePicker設(shè)置默認(rèn)值方式

    antd之RangePicker設(shè)置默認(rèn)值方式

    這篇文章主要介紹了antd之RangePicker設(shè)置默認(rèn)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • React.memo函數(shù)中的參數(shù)示例詳解

    React.memo函數(shù)中的參數(shù)示例詳解

    這篇文章主要為大家介紹了React.memo函數(shù)中的參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • react render props模式實(shí)現(xiàn)組件復(fù)用示例

    react render props模式實(shí)現(xiàn)組件復(fù)用示例

    本文主要介紹了react render props模式實(shí)現(xiàn)組件復(fù)用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論

兴安县| 桑日县| 太康县| 蒲江县| 宁晋县| 盖州市| 涟源市| 乐业县| 闽清县| 涟源市| 涟源市| 承德市| 河西区| 蒲城县| 边坝县| 察隅县| 屏山县| 永安市| 开江县| 碌曲县| 长丰县| 许昌市| 天水市| 平阳县| 阿拉尔市| 永春县| 文安县| 莲花县| 合作市| 大安市| 鄢陵县| 杭州市| 金湖县| 二连浩特市| 滨海县| 临泉县| 稷山县| 岐山县| 乐业县| 永寿县| 长葛市|