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

盐田网站建设重庆电子商务seo

盐田网站建设,重庆电子商务seo,wordpress获取自定义字段值,wordpress301规则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.hkea.cn/news/936150/

相关文章:

  • 做网站用哪几个端口 比较好百度贴吧官网网页
  • 弹幕网站是怎么做的百度在线翻译
  • 独立网站域名代发关键词包收录
  • 如何建立门户网站百度可以发布广告吗
  • 网站开发毕设文献郑州网站优化顾问
  • 青海建设银行的官方网站自己制作网页的网站
  • 网站建设的图片叠加步骤过程2021年搜索引擎排名
  • 网站链接到邮箱怎么做搜索引擎推广试题
  • 十堰城市建设网站制作自己的网站
  • 传媒网站建设公司深圳百度seo优化
  • 公司需要网站 该怎么做公司推广咨询
  • 高中男女做那个视频网站seo做的好的网站
  • 网站建设 摄影服务上海seo网站排名优化公司
  • 网页设计中html代码seo博客网站
  • 怎样建设小游戏网站google关键词搜索技巧
  • 淘宝 客要推广网站怎么做全媒体广告代理
  • 那个b2b网站可以做外贸武汉全网推广
  • 深圳css3网站开发多少钱如何策划一个营销方案
  • 织梦统计网站访问量代码网络营销公司如何建立
  • 外贸营销型网站建设今日最新重大新闻
  • 个性化定制网站长春网络推广优化
  • 合肥庐阳区疫情最新消息seo优化首页
  • h5网站制作接单最新中高风险地区名单
  • 北京市住房城乡建设委网站公司怎么在网上推广
  • 网站建设首页怎样插入视频百度指数在线查询小程序
  • 青州网站制作哪家好aso优化哪家好
  • wordpress做网站优点郑州网站seo优化
  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名
  • 怎么切页面做网站灰色词首页排名接单