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

蚌埠网站制作公司价格wordpress分类链接地址

蚌埠网站制作公司价格,wordpress分类链接地址,互联网公司排名图,北京seo网站设计Context 提供了一个无需为每层组件手动添加 props ,就能在组件树间进行数据传递的方法,useContext 用于函数组件中订阅上层 context 的变更,可以获取上层 context 传递的 value prop 值 useContext 接收一个 context 对象(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 button's 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 button's 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. 方式1: 拆分不会一起更改的 context
  2. 当不能拆分 context 时,将组件一分为二,给中间组件加上 React.memo
  3. 返回一个内置 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.yayakq.cn/news/548385/

相关文章:

  • 东莞网站建设 信科网络济南 规划 网站
  • 平度网站建设公司住房和城乡建设部网站住房补贴
  • 响应式网站建设公司‘重庆欧勒精细有限公司网站策划书
  • 凡科建站网站怎么保存发给别人湖南广告优化
  • 怎样做公司官方网站wordpress token插件
  • 网站建设目标是建设工程项目管理
  • 网站建设基本流程价格全国首批9所重点马院网站建设
  • 域名系统seo排名赚钱
  • 企业网站建设变相收取等级保护费wordpress缩略图
  • 网站建设的岗位名称长春新建设
  • 南约社区网站建设网站建设个人简历
  • 网站改版 301跳转企业营销网站建设系统
  • 没有营业执照可以建设网站网站建设的费用明细
  • asp网站开发上传组建七牛云 wordpress
  • 网站建设 中企动力扬州重庆视频制作公司排名
  • 网站怎么做可以再上面输入文字网站负责人 备案
  • 天津网站建设58店面建设网站的必要性
  • 如何建立一个网站收会员费网站app封装怎么做
  • win7在局域网做网站wordpress 页面 404
  • 不需要备案如何做网站网店推广的常用方法有哪些
  • 制作网站app淮北建设网
  • 智慧城市建设评价网站上海公司注册虚拟地址
  • 做网站私活在哪接市场调研分析报告
  • 谁分享一个免费网站2021品牌设计图片
  • 电子商务网站建设选择题贵州建设监理协会网站
  • wordpress等模版比较深圳seo优化
  • 适合手机的网站网站重定向过多
  • 郑州做设计公司网站wordpress w3c
  • 湖州网站开发公司视频制作流程
  • 电子商务网站建设评价论文怎么创造游戏软件