如下面的代碼所示,App 組件有一個狀態(tài)。這個狀態(tài)的屬性通過 props 傳遞給子組件。但是一旦 App 組件中的狀態(tài)發(fā)生變化,Sortable 組件內(nèi)的子組件就不會更新。但是這個 Sortable 容器之外的子組件會像往常一樣更新(重新渲染)。如何在每次狀態(tài)更改時呈現(xiàn)這些可排序的子組件?這是代碼(簡化):應用程序.js:import Sortable from 'react-sortablejs';import Child from './Child';class App extends React.Component { constructor(props) { super(props); this.state = { text1: "Text1", text2: "Text2" } } render() { return ( <div className="App"> <Sortable> <Child text={this.state.text1} /> <Child text={this.state.text2} /> </Sortable> <Child text={this.state.text1} /> </div> ); }}Child.js:從“反應”導入反應;export default class Child extends React.Component { constructor(props) { super(props); } render() { return (<p>{this.props.text}</p>) }}頁面加載時的樣子:...在狀態(tài)更新后:
如何維護Sortable容器的子組件的狀態(tài)?
胡子哥哥
2021-09-04 17:37:16