React Context - Context.Consumer vs Class.contextType

前提是你 提交于 2019-12-10 09:33:40

问题


I am learning about the somewhat newly introduced React.Context API, but I've noticed some inconsistencies on it's consumption across examples. Some use the original Context.Consumer HOC method, while some (including the React Docs) use the static Class.contextType method. What's the difference and why the inconsistency?


回答1:


Turns out that the static Class.contextType was newly introduced on React v16.6.0, as the Context.Consumer method proved problematic on class components. Also, there does seem to be one major difference between using both, and it's that the static Class.Context only allows you to subscribe to a single context.

The good news is that both of them seem to reliably listen of Context changes which means if you only have a single Context API, then both are good enough.



来源:https://stackoverflow.com/questions/54283509/react-context-context-consumer-vs-class-contexttype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!