這樣做的含義是什麼?componentWillUpdate - 修改nextProps或nextState是否安全?
class Foo extends React.Component {
componentWillUpdate(nextProps, nextState) {
nextState.bar = transformSomehow(nextState.foo);
}
}
我的直覺告訴我,這是錯的,但我還沒有發現任何文件說,nextProps
和nextState
應該是隻讀的,我也沒有發現任何東西說,這是確定的。
這裏的想法是,我想要一些從狀態的數據sanitized /轉換/修改版本。有可能有很多其他(更好)的方法來做到這一點,但這也是一個可行的選擇?
道具應被視爲只讀:https://facebook.github.io/react/docs/components-and-props.html#props-are-read-only – lux
其實我已經驗證了' nextProps'確實是不可修改的。一些getter/setter魔法。對於'nextState'我不能說。 –