设为首页 加入收藏

TOP

从 Vue 的视角学 React(四)—— 组件传参(二)
2019-09-17 19:10:10 】 浏览:80
Tags:Vue 视角 React 组件
n
Toolbar(props) { return ( <div> <ThemedButton /> </div> ); } class ThemedButton extends React.Component { // 指定 contextType 读取当前的 theme context。 // React 会往上找到最近的 theme Provider,然后使用它的值。 // 在这个例子中,当前的 theme 值为 “dark”。 static contextType = ThemeContext; render() { return <Button theme={this.context} />; } }

但 context 的使用会极大的增强组件之间的耦合性,项目中并不建议直接使用

所以我直接复制粘贴了官方文档的代码,仅为了解 context 这个概念

小型项目中,如果有深层次的传参,应当从组件设计上解决问题,比如直接将组件传下去

而大型项目中,如果需要用到 context,更推荐使用 redux 和 mobx 这些成熟的状态管理工具

 

 

参考资料:

《React 状态提升》

《React.js 的 context》

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JavaScript Timing 事件及两种时.. 下一篇jQuery设置样式css

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目