【发布时间】:2021-05-09 10:19:57
【问题描述】:
以下代码位于https://codesandbox.io/s/child-updating-parent-dont-want-pfdxd 我面临的问题是,当我对子组件(page2)进行排序时,它还会更新父组件(App)的状态。我想避免状态更新,因为其他子组件(第 1 页)应该显示未排序的数据。我想过在 App.js 上有两个具有相同数据(两个名称和两个数字)的状态。然后为第 1 页使用一组状态(名称和数字),为第 2 页使用另一组。这似乎是多余和不必要的,但我不确定在第 2 页排序时如何避免状态更新。有没有办法避免在子组件中操作数据时的状态更新?
/*Parent*/
class App extends React.Component {
constructor() {
super();
this.state = {
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
names: ["john", "sally", "bill", "rebecca"]
};
}
render() {
const { numbers, names } = this.state;
return (
<Router>
<div className="App">
<Header />
<Switch>
<Route
path="/page1"
exact
render={(routeProps) => (
<Page1 digits={numbers} people={names} {...routeProps} />
)}
/>
<Route
path="/page2"
exact
render={(routeProps) => (
<Page2 digits={numbers} people={names} {...routeProps} />
)}
/>
</Switch>
</div>
</Router>
);
}
}
export default App;
这是第一个接收道具的组件
/*child 1*/
const page1 = ({ people, digits }) => {
return (
<div>
<h1>
Number Should Be "1" And Name Should Be "john" On This Page Regardless
Of Sort On Page 2
</h1>
<p>Number: {digits[0]}</p>
<p>Name: {people[0]}</p>
</div>
);
};
export default page1;
这是接收道具的第二个组件
/*child 2*/
const Page2 = ({ people, digits }) => {
/* when uncommented the sort will also change state on App.js ---
I want state on App.js to remain in original state aft sorting*/
/* --- UNCOMMENT BELOW TO SORT --- */
/*people.sort((a, b) => b.toLowerCase().localeCompare(a.toLowerCase()));
digits.sort((a,b)=>b-a)*/
return (
<div>
<h1>
Number Should Be "10" And Name Should Be "sally" On This Page When Sort
Is Uncommented
</h1>
<p>Number: {digits[0]}</p>
<p>Name: {people[0]}</p>
</div>
);
};
export default Page2;
【问题讨论】:
-
在 react 中的 props 是不可变的。您正在使用数组
sort方法改变道具。sort不会返回一个新数组,它会改变现有数组,这就是你的状态正在改变的原因。 -
我需要将排序设置为变量吗?关于如何避免的建议?
标签: reactjs react-props react-state