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

(组件的)状态(state)和属性(props)之间有何不同? #4

Open
careteenL opened this issue Apr 17, 2020 · 0 comments
Open

(组件的)状态(state)和属性(props)之间有何不同? #4

careteenL opened this issue Apr 17, 2020 · 0 comments
Labels
每日一问 每天一道面试题

Comments

@careteenL
Copy link
Owner

  • 共同点: props 和 state 都属于 react 数据接口,他们都可以决定组件的行为和状态
  • 不同点:
    • props 属于对外接口,state 属于对内接口,props 主要是父组件传递参数配置该组件,组件内部无法控制也无法修改。想要获取父组件的数据只能通过回调函数的形式(即在自组件中调用父组件的方法)。
    • state 主要用于保存,控制,修改组件自身状态,你可以理解state是一个局部的,只能被组件自身控制的数据源。不能被外界获取和修改。我们可以通过this.setState更新数据,setState导致组件重新渲染。
  • 总之props是让外部对组件进行配置,state是让组件控制自己的状态
  • 工作中:没有state的组件叫无状态组件,反之有状态组件,工作中尽量多无状态组件,尽量少写有状态组件,增加代码的可维护性和复用性。
@careteenL careteenL added the 每日一问 每天一道面试题 label Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
每日一问 每天一道面试题
Projects
None yet
Development

No branches or pull requests

1 participant