在React中強(qiáng)制重新渲染的4 種方式案例代碼
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編寫小程序的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)容,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑
相比于react-dnd,react-beautiful-dnd更適用于列表之間拖拽的場(chǎng)景,本文主要介紹了使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑,感興趣的可以了解一下2021-05-05
react native仿微信PopupWindow效果的實(shí)例代碼
本篇文章主要介紹了react native仿微信PopupWindow效果的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
antd之RangePicker設(shè)置默認(rèn)值方式
這篇文章主要介紹了antd之RangePicker設(shè)置默認(rèn)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
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ù)用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

