当前位置: 首页 > news >正文

沧州建设网站的公司石柱网站制作

沧州建设网站的公司,石柱网站制作,wordpress igoogle,大公司需要seo是什么职业Context 提供了一个无需为每层组件手动添加 props #xff0c;就能在组件树间进行数据传递的方法#xff0c;useContext 用于函数组件中订阅上层 context 的变更#xff0c;可以获取上层 context 传递的 value prop 值 useContext 接收一个 context 对象#xff08;React.…Context 提供了一个无需为每层组件手动添加 props 就能在组件树间进行数据传递的方法useContext 用于函数组件中订阅上层 context 的变更可以获取上层 context 传递的 value prop 值 useContext 接收一个 context 对象React.createContext的返回值并返回 context 的当前值当前的 context 值由上层组件中距离当前组件最近的 MyContext.Provider 的 value prop 决定 const value useContext(MyContext);使用 import React, { useContext, useState } from react;const themes {light: {foreground: #000000,background: #eeeeee},dark: {foreground: #ffffff,background: #222222} };// 为当前 theme 创建一个 context const ThemeContext React.createContext();export default function Toolbar(props) {const [theme, setTheme] useState(themes.dark);const toggleTheme () {setTheme(currentTheme (currentTheme themes.dark? themes.light: themes.dark));};return (// 使用 Provider 将当前 props.value 传递给内部组件ThemeContext.Provider value{{theme, toggleTheme}}ThemeButton //ThemeContext.Provider); }function ThemeButton() {// 通过 useContext 获取当前 context 值const { theme, toggleTheme } useContext(ThemeContext);return (button style{{background: theme.background, color: theme.foreground }} onClick{toggleTheme}Change the buttons theme/button); }等价 class的示例如下 useContext(MyContext) 相当于 class 组件中的 static contextType MyContext 或者 MyContext.Consumer useContext 并没有改变消费 context 的方式它只为我们提供了一种额外的、更漂亮的、更漂亮的方法来消费上层 context。在将其应用于使用多 context 的组件时将会非常有用 import React from react;const themes {light: {foreground: #000000,background: #eeeeee},dark: {foreground: #ffffff,background: #222222} };const ThemeContext React.createContext(themes.light);function ThemeButton() {return (ThemeContext.Consumer{({theme, toggleTheme}) (button style{{background: theme.background, color: theme.foreground }} onClick{toggleTheme}Change the buttons theme/button)}/ThemeContext.Consumer); }export default class Toolbar extends React.Component {constructor(props) {super(props);this.state {theme: themes.light};this.toggleTheme this.toggleTheme.bind(this);}toggleTheme() {this.setState(state ({theme:state.theme themes.dark? themes.light: themes.dark}));}render() {return (ThemeContext.Provider value{{ theme: this.state.theme, toggleTheme: this.toggleTheme }}ThemeButton //ThemeContext.Provider)} }优化消费 context 组件 调用了 useContext 的组件都会在 context 值变化时重新渲染为了减少重新渲染组件的较大开销可以通过使用 memoization 来优化 假设由于某种原因您有 AppContext其值具有 theme 属性并且您只想在 appContextValue.theme 更改上重新渲染一些 ExpensiveTree 方式1: 拆分不会一起更改的 context当不能拆分 context 时将组件一分为二给中间组件加上 React.memo返回一个内置 useMemo 的组件 function Button() {// 把 theme context 拆分出来其他 context 变化时不会导致 ExpensiveTree 重新渲染let theme useContext(ThemeContext);return ExpensiveTree className{theme} /; }function Button() {let appContextValue useContext(AppContext);let theme appContextValue.theme; // 获取 theme 属性return ThemedButton theme{theme} / }const ThemedButton memo(({ theme }) {// 使用 memo 尽量复用上一次渲染结果return ExpensiveTree className{theme} /; });function Button() {let appContextValue useContext(AppContext);let theme appContextValue.theme; // 获取 theme 属性return useMemo(() {// The rest of your rendering logicreturn ExpensiveTree className{theme} /;}, [theme]) }
http://www.hkea.cn/news/14558087/

相关文章:

  • 中国信用网企业查询系统优化裁员
  • 青海网站设计高端博客网站开发流程
  • 西安建设集团网站在线oa
  • 免费建站推广随州做网站
  • 北京网站建设公司报价网站建设方案之目标
  • 虚拟技术对网站建设维护的影响微信app下载安装旧版本
  • 海外网站加速器下载青岛万科蓝山设计公司
  • 南通网站建设心得百度一下官网首页下载
  • 个人网站 备案做网站网站需要
  • 网站服务器架设宁波公司网站开发
  • 企业网站建设的开放方式一般有wordpress上传excel文件
  • 天津做流产五洲网站优化用户体验
  • 三维家3d设计软件免费哈尔滨做网站优化
  • 网站的更新与维护河北邯郸中考成绩公布时间
  • 网站建设中期目标建设网站天河区
  • 开发中英文网站多少钱铁路网站建设
  • 去什么网站做推广网站建设经验与团队
  • 做网页的软件h网站制作优化
  • 网站的规划与建设案例分析广告中国第一
  • 零购物网站怎么建设好看的html代码
  • ppt 如何做网站交互式医院 网站建设
  • 重庆铜梁网站建设报价鞍山58招聘
  • 企业网站推广属于付费推广吗做seo为什么要了解网站
  • 苏州哪家公司做网站雄安网站建设推广
  • 做软装搭配的网站wordpress first主题
  • ps做网站宽度wordpress 图片保存在哪
  • 系统门窗品牌10大品牌排行互联网seo是什么
  • 网站托管运营wow亚洲服有永久60级么
  • 毕业设计做网站怎么样金蝶直播软件
  • wordpress产品模块上海公司网站seo