简体   繁体   中英

Why is an anti-pattern letting a react component to edit its own props?

我正在寻找另一个问题的答案,我找到了这个答案( 链接 )我只是好奇为什么修改自己的道具是反模式,为什么不修改自己的状态不是和反模式?

In react, props is an object of data that is unlikely to change in the lifecycle of the component, and state is data that is likely to change in the lifecycle of the component.

It helps create explicit rules for where developers should put things that do/do-not change.

React update pages very quickly by knowing exactly that data changes will either come strictly from a parent(as props) or come internally as state.

the exact function checks loosely if state/props are the same, then https://github.com/facebook/react/blob/master/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L881-L883

I'd recommend reading about how react "reconciles" data for changes and making updates to the dom. https://facebook.github.io/react/docs/reconciliation.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM