在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)用戶點擊標題時,我們想要重新掛載/卸載整個子組件,這時可以在子組件上增加一個 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結(jié)合Drag?API實現(xiàn)拖拽示例詳解
這篇文章主要為大家介紹了React結(jié)合Drag?API實現(xiàn)拖拽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03React Hook - 自定義Hook的基本使用和案例講解
自定義Hook本質(zhì)上只是一種函數(shù)代碼邏輯的抽取,嚴格意義上來說,它本身并不算React的特性,這篇文章主要介紹了React類組件和函數(shù)組件對比-Hooks的介紹及初體驗,需要的朋友可以參考下2022-11-11React 項目中動態(tài)設(shè)置環(huán)境變量
本文主要介紹了React 項目中動態(tài)設(shè)置環(huán)境變量,本文將介紹兩種常用的方法,使用 dotenv 庫和通過命令行參數(shù)傳遞環(huán)境變量,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04