Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day364:为什么说 React 中 props 是只读的? #1197

Open
Genzhen opened this issue Aug 12, 2021 · 2 comments
Open

Day364:为什么说 React 中 props 是只读的? #1197

Genzhen opened this issue Aug 12, 2021 · 2 comments
Labels
React teach_tag

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Aug 12, 2021

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解

二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。


Props

React 官方文档说法:

React is pretty flexible but it has a single strict rule: all React components must act like pure functions with respect to their props.

props 是组件的只读属性,组件内部不能直接修改 props,要想修改 props,只能在该组件的上层组件中修改

在 React 中,props 是上层组件传递进来的值,这个值是父类的值,同 Vue 一样,props 的传值是单项数据流,也就是不会让影响父类的值,如果需要改值,可以先让 props 赋值给一个变量,在修改这个变量。

其实就是保证 React 单向数据流的设计模式,使状态可预测。

如果允许子组件修改,那么一个父组件将状态传递给好几个子组件,这几个子组件随意修改,就完全不可预测,不知道在什么地方修改了状态。

@Genzhen Genzhen added the React teach_tag label Aug 12, 2021
@DaphnisLi
Copy link

单向数据流

@Wangjian1211
Copy link

二维码链接跳转失败
二维码图片展示失败

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React teach_tag
Projects
None yet
Development

No branches or pull requests

3 participants