We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
[react] 说说Context有哪些属性?
The text was updated successfully, but these errors were encountered:
简单介绍下Context: Context 提供了一个无需为每层组件手动添加 props,就能在组件树间进行数据传递的方法。我们在平时开发中如果不使用redux 这种数据状态管理库,可能在数据传递的时候使用的就是从祖先元素层层传递的方式,当层级较多之后,我们需要将数据通过组件的props接口层层传递,层级如果嵌套太深,总会容易出错。Context 就是为了解决这个问题而出现的。
Context
props
需要谨慎使用,因为这样会让组件的复用性变差。
Sorry, something went wrong.
react.createContext(); Context.Provider(); Context.Consumer(); Context.displayName();
https://react.docschina.org/docs/context.html#api
context属于一种解决组件间层级过多传递数据的问题,避免了层层嵌套的通过props传递的形式,同时对于不需要使用到redux时,是一种解决方案,关于组件的复用性变差的问题,我觉得是可以通过高阶组件和context配合来解决的,因为react-redux使用的就是这样的形式; 主要的形式:createContext(value):创建一个context实例;其中的参数为当前数据的默认值,只有没在Provider中指定value时,才会生效; Context.Provider:生产者,数据提供方;通过value属性来定义需要被传递的数据 Context.Consumer:消费者,数据获取方;根据是函数组件还是class组件,有不同的使用形式;class组件可以指定contextType来确定要使用哪一个context对象的值,函数组件需要使用回调函数的形式来获取context的值;需要显示的指定context对象;
No branches or pull requests
[react] 说说Context有哪些属性?
The text was updated successfully, but these errors were encountered: