在React中強制重新渲染的4 種方式案例代碼
1.在 state 改變時重新渲染組件
React 組件在每次 state 變化時都會運行 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>
</>
);
}
}以上都會輸出如下:
render() method
render() method
2.在 props 改變時重新渲染組件
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,但它接收了一個 prop 名為 message。
點擊按鈕后,會更新 <Child /> 組件,會引起 render() 再次執(zhí)行。
Child component: render() Child component: render()
3.借助 key prop 重新渲染
上述更新 state 和 props 的操作不會引起組件的重新掛載/卸載,只會重新調(diào)用 render() 方法。有時候?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
: ""
}
/>
</>
);
}
}上述的這個例子,當(dāng)用戶點擊標(biāo)題時,我們想要重新掛載/卸載整個子組件,這時可以在子組件上增加一個 key 屬性,這樣便實現(xiàn)了目的??梢钥吹矫看吸c擊后,都會執(zhí)行 componentWillUnmount() 方法。
4.強制重新渲染
不建議采用此方式,建議采用更新 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 中強制重新渲染的 4 種方式的文章就介紹到這了,更多相關(guān)React 重新渲染內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
采用React編寫小程序的Remax框架的編譯流程解析(推薦)
這篇文章主要介紹了采用React編寫小程序的Remax框架的編譯流程解析(推薦),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
react-navigation之動態(tài)修改title的內(nèi)容
這篇文章主要介紹了react-navigation之動態(tài)修改title的內(nèi)容,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
使用react-beautiful-dnd實現(xiàn)列表間拖拽踩坑
相比于react-dnd,react-beautiful-dnd更適用于列表之間拖拽的場景,本文主要介紹了使用react-beautiful-dnd實現(xiàn)列表間拖拽踩坑,感興趣的可以了解一下2021-05-05
react native仿微信PopupWindow效果的實例代碼
本篇文章主要介紹了react native仿微信PopupWindow效果的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08
React.memo函數(shù)中的參數(shù)示例詳解
這篇文章主要為大家介紹了React.memo函數(shù)中的參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
react render props模式實現(xiàn)組件復(fù)用示例
本文主要介紹了react render props模式實現(xiàn)組件復(fù)用示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

